bcachefs: early return for negative values when parsing BCH_OPT_UINT
authorIntegral <integral@archlinuxcn.org>
Sun, 6 Apr 2025 14:53:28 +0000 (22:53 +0800)
committerKent Overstreet <kent.overstreet@linux.dev>
Thu, 22 May 2025 00:14:07 +0000 (20:14 -0400)
Currently, when passing a negative integer as argument, the error
message is "too big" due to casting to an unsigned integer:

    > bcachefs format --block_size=-1 bcachefs.img
    invalid option: block_size: too big (max 65536)

When negative value in argument detected, return early before
calling bch2_opt_validate().

A new error code `BCH_ERR_option_negative` is added.

Signed-off-by: Integral <integral@archlinuxcn.org>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/errcode.h
fs/bcachefs/opts.c

index d9ebffa5b3a28286f45a6dad67819d0c7bb811dd..768b176f6ea8e222afb9b274e7e065dccfe99de5 100644 (file)
        x(EINVAL,                       inode_unpack_error)                     \
        x(EINVAL,                       varint_decode_error)                    \
        x(EINVAL,                       erasure_coding_found_btree_node)        \
+       x(EINVAL,                       option_negative)                        \
        x(EOPNOTSUPP,                   may_not_use_incompat_feature)           \
        x(EROFS,                        erofs_trans_commit)                     \
        x(EROFS,                        erofs_no_writes)                        \
index af3258814822d9982cb9ef2e1cb0dbfe744471b3..f40de111e5276eb23c8b75663d421df02ea85a85 100644 (file)
@@ -360,9 +360,15 @@ int bch2_opt_parse(struct bch_fs *c,
                        return -EINVAL;
                }
 
-               ret = opt->flags & OPT_HUMAN_READABLE
-                       ? bch2_strtou64_h(val, res)
-                       : kstrtou64(val, 10, res);
+               if (*val != '-') {
+                       ret = opt->flags & OPT_HUMAN_READABLE
+                           ? bch2_strtou64_h(val, res)
+                           : kstrtou64(val, 10, res);
+               } else {
+                       prt_printf(err, "%s: must be a non-negative number", opt->attr.name);
+                       return -BCH_ERR_option_negative;
+               }
+
                if (ret < 0) {
                        if (err)
                                prt_printf(err, "%s: must be a number",