From: Byongho Lee <bhlee.kernel@gmail.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 1/3] btrfs-progs: fix memory leak in btrfs-convert main()
Date: Fri, 28 Aug 2015 00:38:16 +0900 [thread overview]
Message-ID: <1440689898-35178-2-git-send-email-bhlee.kernel@gmail.com> (raw)
In-Reply-To: <1440689898-35178-1-git-send-email-bhlee.kernel@gmail.com>
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>
---
btrfs-convert.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/btrfs-convert.c b/btrfs-convert.c
index 917bbc1b74d2..25ae424ea73b 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);
--
2.5.0
next prev parent reply other threads:[~2015-08-27 15:38 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-27 15:38 [PATCH 0/3] btrfs-progs: Small fixes for code using strdup() Byongho Lee
2015-08-27 15:38 ` Byongho Lee [this message]
2015-08-31 15:06 ` [PATCH 1/3] btrfs-progs: fix memory leak in btrfs-convert main() David Sterba
2015-08-27 15:38 ` [PATCH 2/3] btrfs-progs: fix memory leak in btrfs-map-logical main() Byongho Lee
2015-08-28 17:26 ` David Sterba
2015-08-27 15:38 ` [PATCH 3/3] btrfs-progs: add memory allocation fail check in btrfs_add_to_fsid() Byongho Lee
2015-08-28 17:28 ` David Sterba
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1440689898-35178-2-git-send-email-bhlee.kernel@gmail.com \
--to=bhlee.kernel@gmail.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).