Skip to contentSkip to conversation

bcachefs: return early for negative values when parsing BCH_OPT_UINT

PATCH Superseded Integral 2 participants 4 messages Apr 6, 2025

lore
Share & tools
lore

Integral

author
#0 lore raw

PATCH 1/1 bcachefs: return early for negative values when parsing BCH_OPT_UINT

Currently, when a negative integer is passed as an argument, the error message incorrectly states "too big" due to the value being cast to an unsigned integer:

Quoted 1 line

bcachefs format --block_size=-1 bcachefs.img

invalid option: block_size: too big (max 65536)

To resolve this issue, return early for negative values before calling bch2_opt_validate().

---
 fs/bcachefs/opts.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
fs/bcachefs/opts.c +9 -3
index e64777ecf44f..31f90c177c6c 100644
@@ -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 -EINVAL;
+		}
+
 		if (ret < 0) {
 			if (err)
 				prt_printf(err, "%s: must be a number",
Signature · 1 line
2.49.0
#1 lore raw
Quoted 11 lines · Integral

Currently, when a negative integer is passed as an argument, the error message incorrectly states "too big" due to the value being cast to an unsigned integer:

Quoted 1 line

bcachefs format --block_size=-1 bcachefs.img

invalid option: block_size: too big (max 65536)

To resolve this issue, return early for negative values before calling bch2_opt_validate().

No new -EINVAL in kernel code :)

Quoted 30 lines
---
 fs/bcachefs/opts.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
fs/bcachefs/opts.c +9 -3
index e64777ecf44f..31f90c177c6c 100644
@@ -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 -EINVAL;
+		}
+
 		if (ret < 0) {
 			if (err)
 				prt_printf(err, "%s: must be a number",
Signature · 1 line
2.49.0
author
#2 lore raw

PATCH v2 1/1 bcachefs: early return for negative values when parsing BCH_OPT_UINT

Currently, when passing a negative integer as argument, the error message is "too big" due to casting to an unsigned integer:

Quoted 1 line

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.

---
 fs/bcachefs/errcode.h |  1 +
 fs/bcachefs/opts.c    | 12 +++++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)
fs/bcachefs/errcode.h +1 -0
index c4eb0ed9838d..e3c85288fd6d 100644
@@ -213,6 +213,7 @@
 	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)			\
fs/bcachefs/opts.c +9 -3
index e64777ecf44f..1bf2580ab735 100644
@@ -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",
Signature · 1 line
2.49.0
#3 lore raw

Re: [PATCH v2] bcachefs: early return for negative values when parsing BCH_OPT_UINT

Quoted 4 lines · Integral

Currently, when passing a negative integer as argument, the error message is "too big" due to casting to an unsigned integer:

Quoted 1 line

bcachefs format --block_size=-1 bcachefs.img

Quoted 8 lines

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.

Applied

Quoted 43 lines
---
 fs/bcachefs/errcode.h |  1 +
 fs/bcachefs/opts.c    | 12 +++++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)
fs/bcachefs/errcode.h +1 -0
index c4eb0ed9838d..e3c85288fd6d 100644
@@ -213,6 +213,7 @@
 	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)			\
fs/bcachefs/opts.c +9 -3
index e64777ecf44f..1bf2580ab735 100644
@@ -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",
Signature · 1 line
2.49.0