diff mbox series

[1/3] btrfs-progs: subvolume create: handle failure for strdup()

Message ID 8c3df11d55b5add76a6abfd7896762697520a136.1698903010.git.wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: subvolume create: accept multiple arguments | expand

Commit Message

Qu Wenruo Nov. 2, 2023, 5:33 a.m. UTC
The function strdup() can return NULL if the system is out of memory,
thus we need to hanle the rare but possible -ENOMEM case.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 cmds/subvolume.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

David Sterba Nov. 23, 2023, 7:11 p.m. UTC | #1
On Thu, Nov 02, 2023 at 04:03:48PM +1030, Qu Wenruo wrote:
> The function strdup() can return NULL if the system is out of memory,
> thus we need to hanle the rare but possible -ENOMEM case.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
>  cmds/subvolume.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/cmds/subvolume.c b/cmds/subvolume.c
> index 8504c380c9ee..bccc4968dad3 100644
> --- a/cmds/subvolume.c
> +++ b/cmds/subvolume.c
> @@ -194,8 +194,17 @@ static int cmd_subvolume_create(const struct cmd_struct *cmd, int argc, char **a
>  	}
>  
>  	dupname = strdup(dst);
> +	if (!dupname) {
> +		error("out of memory when duplicating %s", dst);

There are message templats for the most common errors, so this is now

error_msg(ERROR_MSG_MEMORY, "duplicating %s", dst);
diff mbox series

Patch

diff --git a/cmds/subvolume.c b/cmds/subvolume.c
index 8504c380c9ee..bccc4968dad3 100644
--- a/cmds/subvolume.c
+++ b/cmds/subvolume.c
@@ -194,8 +194,17 @@  static int cmd_subvolume_create(const struct cmd_struct *cmd, int argc, char **a
 	}
 
 	dupname = strdup(dst);
+	if (!dupname) {
+		error("out of memory when duplicating %s", dst);
+		goto out;
+	}
 	newname = basename(dupname);
+
 	dupdir = strdup(dst);
+	if (!dupdir) {
+		error("out of memory when duplicating %s", dst);
+		goto out;
+	}
 	dstdir = dirname(dupdir);
 
 	if (!test_issubvolname(newname)) {