summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorByongho Lee <bhlee.kernel@gmail.com>2015-08-28 00:38:16 +0900
committerDavid Sterba <dsterba@suse.com>2015-09-01 14:02:48 +0200
commit9a99d2b683c23cbfb21df0557fa185b36e9e8540 (patch)
tree04f711e286ec5364f831fa43ecc5656a2b708c51
parent107adbd05d037567d20211732e5c9039853319ed (diff)
btrfs-progs: fix memory leak in btrfs-convert main()
In btrfs-convert main(), strdup() allocates memory to fslabel but that memory is not freed. We could fix it by adding free() calls to every return point, but that would make the code messy because there are several return paths. So I fix it by changing the code using strdup() with local array and strncpy(). And btrfs-convert main() guarantees that string length of fslabel is not to exceed 'BTRFS_LABEL_SIZE', so it's enough to use strcpy() instead of strncpy() to copy fslabel in do_convert(). Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com> Signed-off-by: David Sterba <dsterba@suse.com>
-rw-r--r--btrfs-convert.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/btrfs-convert.c b/btrfs-convert.c
index 917bbc1b..2e6f4d4c 100644
--- a/btrfs-convert.c
+++ b/btrfs-convert.c
@@ -2428,7 +2428,7 @@ static int do_convert(const char *devname, int datacsum, int packing, int noxatt
fprintf(stderr, "copy label '%s'\n",
root->fs_info->super_copy->label);
} else if (copylabel == -1) {
- strncpy(root->fs_info->super_copy->label, fslabel, BTRFS_LABEL_SIZE);
+ strcpy(root->fs_info->super_copy->label, fslabel);
fprintf(stderr, "set label to '%s'\n", fslabel);
}
@@ -2868,7 +2868,7 @@ int main(int argc, char *argv[])
int usage_error = 0;
int progress = 1;
char *file;
- char *fslabel = NULL;
+ char fslabel[BTRFS_LABEL_SIZE + 1];
u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
while(1) {
@@ -2910,8 +2910,9 @@ int main(int argc, char *argv[])
break;
case 'l':
copylabel = -1;
- fslabel = strdup(optarg);
- if (strlen(fslabel) > BTRFS_LABEL_SIZE) {
+ fslabel[BTRFS_LABEL_SIZE] = 0;
+ strncpy(fslabel, optarg, sizeof(fslabel));
+ if (fslabel[BTRFS_LABEL_SIZE]) {
fprintf(stderr,
"warning: label too long, trimmed to %d bytes\n",
BTRFS_LABEL_SIZE);