From: Byongho Lee <bhlee.kernel@gmail.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 3/3] btrfs-progs: add memory allocation fail check in btrfs_add_to_fsid()
Date: Fri, 28 Aug 2015 00:38:18 +0900 [thread overview]
Message-ID: <1440689898-35178-4-git-send-email-bhlee.kernel@gmail.com> (raw)
In-Reply-To: <1440689898-35178-1-git-send-email-bhlee.kernel@gmail.com>
In btrfs_add_to_fsid(), strdup() allocates memory to device->name, but
the return value is not checked.
So add the return value check and error handling code.
And clean-up error handling code for ENOMEM case.
Signed-off-by: Byongho Lee <bhlee.kernel@gmail.com>
---
utils.c | 20 ++++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/utils.c b/utils.c
index 280637d896c1..160a8c273da8 100644
--- a/utils.c
+++ b/utils.c
@@ -729,21 +729,18 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
struct btrfs_super_block *super = root->fs_info->super_copy;
struct btrfs_device *device;
struct btrfs_dev_item *dev_item;
- char *buf;
+ char *buf = NULL;
u64 total_bytes;
u64 num_devs;
int ret;
device = kzalloc(sizeof(*device), GFP_NOFS);
if (!device)
- return -ENOMEM;
- buf = kmalloc(sectorsize, GFP_NOFS);
- if (!buf) {
- kfree(device);
- return -ENOMEM;
- }
+ goto err_nomem;
+ buf = kzalloc(sectorsize, GFP_NOFS);
+ if (!buf)
+ goto err_nomem;
BUG_ON(sizeof(*disk_super) > sectorsize);
- memset(buf, 0, sectorsize);
disk_super = (struct btrfs_super_block *)buf;
dev_item = &disk_super->dev_item;
@@ -761,6 +758,8 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
device->total_ios = 0;
device->dev_root = root->fs_info->dev_root;
device->name = strdup(path);
+ if (!device->name)
+ goto err_nomem;
ret = btrfs_add_device(trans, root, device);
BUG_ON(ret);
@@ -790,6 +789,11 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
device->fs_devices = root->fs_info->fs_devices;
return 0;
+
+err_nomem:
+ kfree(device);
+ kfree(buf);
+ return -ENOMEM;
}
static void btrfs_wipe_existing_sb(int fd)
--
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 ` [PATCH 1/3] btrfs-progs: fix memory leak in btrfs-convert main() Byongho Lee
2015-08-31 15:06 ` 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 ` Byongho Lee [this message]
2015-08-28 17:28 ` [PATCH 3/3] btrfs-progs: add memory allocation fail check in btrfs_add_to_fsid() 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-4-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.