Fix coverity defects: CID 147707

coverity scan CID:147707, Type:Double free.

Reviewed-by: Richard Laager <rlaager@wiktel.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: cao.xuewen <cao.xuewen@zte.com.cn>
Closes #5097
This commit is contained in:
cao 2016-10-01 01:49:16 +08:00 committed by Brian Behlendorf
parent ec009855c4
commit 8047715ab4

View File

@ -72,7 +72,7 @@ smb_retrieve_shares(void)
{
int rc = SA_OK;
char file_path[PATH_MAX], line[512], *token, *key, *value;
char *dup_value, *path = NULL, *comment = NULL, *name = NULL;
char *dup_value = NULL, *path = NULL, *comment = NULL, *name = NULL;
char *guest_ok = NULL;
DIR *shares_dir;
FILE *share_file_fp = NULL;
@ -136,12 +136,19 @@ smb_retrieve_shares(void)
goto out;
}
if (strcmp(key, "path") == 0)
if (strcmp(key, "path") == 0) {
free(path);
path = dup_value;
if (strcmp(key, "comment") == 0)
} else if (strcmp(key, "comment") == 0) {
free(comment);
comment = dup_value;
if (strcmp(key, "guest_ok") == 0)
} else if (strcmp(key, "guest_ok") == 0) {
free(guest_ok);
guest_ok = dup_value;
} else
free(dup_value);
dup_value = NULL;
if (path == NULL || comment == NULL || guest_ok == NULL)
continue; /* Incomplete share definition */
@ -153,25 +160,24 @@ smb_retrieve_shares(void)
goto out;
}
strncpy(shares->name, name,
(void) strlcpy(shares->name, name,
sizeof (shares->name));
shares->name [sizeof (shares->name) - 1] = '\0';
strncpy(shares->path, path,
(void) strlcpy(shares->path, path,
sizeof (shares->path));
shares->path [sizeof (shares->path) - 1] = '\0';
strncpy(shares->comment, comment,
(void) strlcpy(shares->comment, comment,
sizeof (shares->comment));
shares->comment[sizeof (shares->comment)-1] =
'\0';
shares->guest_ok = atoi(guest_ok);
shares->next = new_shares;
new_shares = shares;
name = NULL;
free(path);
free(comment);
free(guest_ok);
path = NULL;
comment = NULL;
guest_ok = NULL;
@ -179,13 +185,20 @@ smb_retrieve_shares(void)
}
out:
if (share_file_fp != NULL)
if (share_file_fp != NULL) {
fclose(share_file_fp);
share_file_fp = NULL;
}
free(name);
free(path);
free(comment);
free(guest_ok);
name = NULL;
path = NULL;
comment = NULL;
guest_ok = NULL;
}
closedir(shares_dir);