aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorcao <[email protected]>2016-10-01 01:49:16 +0800
committerBrian Behlendorf <[email protected]>2016-09-30 10:49:16 -0700
commit8047715ab40836a27b742a808d6c2a0496478bd4 (patch)
treed631afe12f0942d4ffd74eea3d58e95d0fc09ac5 /lib
parentec009855c4a43c83837741f787ab26cb7f8edfdc (diff)
Fix coverity defects: CID 147707
coverity scan CID:147707, Type:Double free. Reviewed-by: Richard Laager <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: cao.xuewen <[email protected]> Closes #5097
Diffstat (limited to 'lib')
-rw-r--r--lib/libshare/smb.c41
1 files changed, 27 insertions, 14 deletions
diff --git a/lib/libshare/smb.c b/lib/libshare/smb.c
index 1ac1a8d27..f8b7118a3 100644
--- a/lib/libshare/smb.c
+++ b/lib/libshare/smb.c
@@ -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,
- 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;
@@ -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);