Skip to content

Commit

Permalink
Fix coverity defects: CID 147707
Browse files Browse the repository at this point in the history
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
  • Loading branch information
heary-cao authored and behlendorf committed Sep 30, 2016
1 parent ec00985 commit 8047715
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions lib/libshare/smb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand All @@ -153,39 +160,45 @@ smb_retrieve_shares(void)
goto out;
}

strncpy(shares->name, name,
sizeof (shares->name));
shares->name [sizeof (shares->name) - 1] = '\0';
(void) strlcpy(shares->name, name,
sizeof (shares->name));

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;
}
}

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);

Expand Down

0 comments on commit 8047715

Please sign in to comment.