]> git.hungrycats.org Git - btrfs-progs/commitdiff
btrfs-progs: handle strdup() failures
authorDavid Sterba <dsterba@suse.com>
Wed, 22 Jul 2026 11:13:02 +0000 (13:13 +0200)
committerQu Wenruo <wqu@suse.com>
Mon, 3 Aug 2026 00:44:50 +0000 (10:14 +0930)
There are many unhandled calls to strdup(), while this almost never
fails we still want to handle all runtime errors.

Signed-off-by: David Sterba <dsterba@suse.com>
cmds/receive.c
cmds/subvolume-list.c
cmds/subvolume.c
crypto/hash-speedtest.c
mkfs/main.c
tests/fssum.c

index 3e3b989c12d48adf07a17c6a142ac494a0400366..8cf01367931befdf022bb974719c66ca2b002f71 100644 (file)
@@ -1560,6 +1560,10 @@ static int do_receive(struct btrfs_receive *rctx, const char *tomnt,
                if (bconf.verbose > BTRFS_BCONF_QUIET)
                        fprintf(stderr, "Chroot to %s\n", dest_dir_full_path);
                rctx->root_path = strdup("/");
+               if (!rctx->root_path) {
+                       ret = -ENOMEM;
+                       goto out;
+               }
                rctx->dest_dir_path = rctx->root_path;
        } else {
                /*
index 0b05f73d0f9ce2c3a654122260e15c644e1cdc06..10cf2100c1643f0b5bbe9556cde493559ddcdba7 100644 (file)
@@ -728,6 +728,8 @@ static int resolve_root(struct rb_root *rl, struct root_info *ri,
                        len += add_len + 1;
                } else {
                        full_path = strdup(found->path);
+                       if (!full_path)
+                               return -ENOMEM;
                        len = add_len;
                }
                if (!ri->top_id)
index a38ad08e87688ccf217849b3781d1b9fecfc6772..8f529189b367f5639df221d979b6ee6ce14de3cb 100644 (file)
@@ -457,8 +457,18 @@ again:
                goto out;
        }
        dupdname = strdup(cpath);
+       if (!dupdname) {
+               error_mem("duplicate path");
+               ret = -ENOMEM;
+               goto out;
+       }
        dname = path_dirname(dupdname);
        dupvname = strdup(cpath);
+       if (!dupvname) {
+               error_mem("duplicate path");
+               ret = -ENOMEM;
+               goto out;
+       }
        vname = path_basename(dupvname);
        free(cpath);
 
@@ -713,6 +723,10 @@ static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char *
                const char *newname;
 
                dupname = strdup(subvol);
+               if (!dupname) {
+                       error_mem("duplicate subvolume");
+                       goto out;
+               }
                newname = path_basename(dupname);
 
                dstdir = malloc(strlen(dst) + 1 + strlen(newname) + 1);
@@ -730,6 +744,10 @@ static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char *
                free(dupname);
        } else {
                dstdir = strdup(dst);
+               if (!dstdir) {
+                       error_mem(NULL);
+                       goto out;
+               }
        }
 
        err = btrfs_util_subvolume_snapshot(subvol, dstdir, flags, NULL, inherit);
@@ -1621,6 +1639,11 @@ static int cmd_subvolume_show(const struct cmd_struct *cmd, int argc, char **arg
        if (subvol.id == BTRFS_FS_TREE_OBJECTID) {
                free(subvol_path);
                subvol_path = strdup("/");
+               if (!subvol_path) {
+                       error_mem(NULL);
+                       ret = -ENOMEM;
+                       goto out;
+               }
                subvol_name = "<FS_TREE>";
        } else {
                subvol_name = path_basename(subvol_path);
index 941de355b61c844d361e8b982b8f0200ee934bf7..6caa89f7d6f88eb4ab45fa399861fbfd384f443e 100644 (file)
@@ -257,6 +257,10 @@ int main(int argc, char **argv) {
                case 'f':
                        free(filter);
                        filter = strdup(optarg);
+                       if (!filter) {
+                               error_mem("filter");
+                               return 1;
+                       }
                        printf("Use filter: %s\n", filter);
                        break;
                case 't':
index f59df0e3cfa161afda90a02eec2a9bd917fbbffa..71c7968310928648e6d768a769085ed81ba3b309 100644 (file)
@@ -1634,6 +1634,10 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
                                        exit(1);
                                }
                                label = strdup(optarg);
+                               if (!label) {
+                                       error_mem("option label");
+                                       exit(1);
+                               }
                                break;
                        case 'm':
                                ret = parse_bg_profile(optarg, &metadata_profile);
@@ -1650,6 +1654,12 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
                                char *orig = strdup(optarg);
                                char *tmp = orig;
 
+                               if (!orig) {
+                                       error_mem("option features");
+                                       ret = 1;
+                                       goto error;
+                               }
+
                                tmp = btrfs_parse_fs_features(tmp, &features);
                                if (tmp) {
                                        error("unrecognized filesystem feature '%s'",
@@ -1670,6 +1680,12 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
                                char *orig = strdup(optarg);
                                char *tmp = orig;
 
+                               if (!orig) {
+                                       error_mem("option runtime features");
+                                       ret = 1;
+                                       goto error;
+                               }
+
                                warning("runtime features are deprecated, use -O, --features instead");
                                tmp = btrfs_parse_runtime_features(tmp,
                                                &features);
@@ -1704,6 +1720,11 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
                        case 'r':
                                free(source_dir);
                                source_dir = strdup(optarg);
+                               if (!source_dir) {
+                                       error_mem("option source directory");
+                                       ret = 1;
+                                       goto error;
+                               }
                                break;
                        case 'u':
                                ret = parse_subvolume(optarg, &subvols, &has_default_subvol);
index 40e54192b1a842402cdcb100a86fd305138c539f..a792decb35307434403584dc04c35c80aadb184e 100644 (file)
@@ -479,6 +479,10 @@ check_manifest(char *fn, char *m, char *c, int last_call)
 
                        /* final cs */
                        checksum = strdup(l);
+                       if (!checksum) {
+                               fprintf(stderr, "allocating checksum\n");
+                               exit(1);
+                       }
                        break;
                }
                if (rem_c == l) {
@@ -502,8 +506,20 @@ malformed:
                } else if (cmp > 0) {
                        excess_file(fn);
                        prev_fn = strdup(l);
+                       if (!prev_fn) {
+                               fprintf(stderr, "memory allocation error\n");
+                               exit(1);
+                       }
                        prev_m = strdup(rem_m);
+                       if (!prev_m) {
+                               fprintf(stderr, "memory allocation error\n");
+                               exit(1);
+                       }
                        prev_c = strdup(rem_c); 
+                       if (!prev_c) {
+                               fprintf(stderr, "memory allocation error\n");
+                               exit(1);
+                       }
                        return;
                }
                missing_file(l);
@@ -836,6 +852,10 @@ main(int argc, char *argv[])
                        if (checksum)
                                free(checksum);
                        checksum = strdup(p);
+                       if (!checksum) {
+                               fprintf(stderr, "memory allocation error\n");
+                               exit(1);
+                       }
                } else {
                        fprintf(stderr, "invalid input file format\n");
                        exit(-1);