Linux bcachefs list
 help / color / mirror / Atom feed
* [PATCH] bcachefs: return early for negative values when parsing BCH_OPT_UINT
@ 2025-04-06 13:59 Integral
  2025-04-06 14:11 ` Kent Overstreet
  0 siblings, 1 reply; 4+ messages in thread
From: Integral @ 2025-04-06 13:59 UTC (permalink / raw)
  To: Kent Overstreet, Kent Overstreet; +Cc: linux-bcachefs, linux-kernel, 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:

    > 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().

Signed-off-by: Integral <integral@archlinuxcn.org>
---
 fs/bcachefs/opts.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/fs/bcachefs/opts.c b/fs/bcachefs/opts.c
index e64777ecf44f..31f90c177c6c 100644
--- a/fs/bcachefs/opts.c
+++ b/fs/bcachefs/opts.c
@@ -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",
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] bcachefs: return early for negative values when parsing BCH_OPT_UINT
  2025-04-06 13:59 [PATCH] bcachefs: return early for negative values when parsing BCH_OPT_UINT Integral
@ 2025-04-06 14:11 ` Kent Overstreet
  2025-04-06 14:53   ` [PATCH v2] bcachefs: early return " Integral
  0 siblings, 1 reply; 4+ messages in thread
From: Kent Overstreet @ 2025-04-06 14:11 UTC (permalink / raw)
  To: Integral; +Cc: Kent Overstreet, linux-bcachefs, linux-kernel

On Sun, Apr 06, 2025 at 09:59:30PM +0800, Integral wrote:
> 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:
> 
>     > 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().
> 
> Signed-off-by: Integral <integral@archlinuxcn.org>

No new -EINVAL in kernel code :)

> ---
>  fs/bcachefs/opts.c | 12 +++++++++---
>  1 file changed, 9 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/bcachefs/opts.c b/fs/bcachefs/opts.c
> index e64777ecf44f..31f90c177c6c 100644
> --- a/fs/bcachefs/opts.c
> +++ b/fs/bcachefs/opts.c
> @@ -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",
> -- 
> 2.49.0
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] bcachefs: early return for negative values when parsing BCH_OPT_UINT
  2025-04-06 14:11 ` Kent Overstreet
@ 2025-04-06 14:53   ` Integral
  2025-04-06 17:26     ` Kent Overstreet
  0 siblings, 1 reply; 4+ messages in thread
From: Integral @ 2025-04-06 14:53 UTC (permalink / raw)
  To: kent.overstreet; +Cc: integral, kent.overstreet, linux-bcachefs, linux-kernel

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>
---
 fs/bcachefs/errcode.h |  1 +
 fs/bcachefs/opts.c    | 12 +++++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/fs/bcachefs/errcode.h b/fs/bcachefs/errcode.h
index c4eb0ed9838d..e3c85288fd6d 100644
--- a/fs/bcachefs/errcode.h
+++ b/fs/bcachefs/errcode.h
@@ -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)			\
diff --git a/fs/bcachefs/opts.c b/fs/bcachefs/opts.c
index e64777ecf44f..1bf2580ab735 100644
--- a/fs/bcachefs/opts.c
+++ b/fs/bcachefs/opts.c
@@ -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",
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] bcachefs: early return for negative values when parsing BCH_OPT_UINT
  2025-04-06 14:53   ` [PATCH v2] bcachefs: early return " Integral
@ 2025-04-06 17:26     ` Kent Overstreet
  0 siblings, 0 replies; 4+ messages in thread
From: Kent Overstreet @ 2025-04-06 17:26 UTC (permalink / raw)
  To: Integral; +Cc: kent.overstreet, linux-bcachefs, linux-kernel

On Sun, Apr 06, 2025 at 10:53:28PM +0800, Integral wrote:
> 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>

Applied

> ---
>  fs/bcachefs/errcode.h |  1 +
>  fs/bcachefs/opts.c    | 12 +++++++++---
>  2 files changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/bcachefs/errcode.h b/fs/bcachefs/errcode.h
> index c4eb0ed9838d..e3c85288fd6d 100644
> --- a/fs/bcachefs/errcode.h
> +++ b/fs/bcachefs/errcode.h
> @@ -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)			\
> diff --git a/fs/bcachefs/opts.c b/fs/bcachefs/opts.c
> index e64777ecf44f..1bf2580ab735 100644
> --- a/fs/bcachefs/opts.c
> +++ b/fs/bcachefs/opts.c
> @@ -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",
> -- 
> 2.49.0
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-04-06 17:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-06 13:59 [PATCH] bcachefs: return early for negative values when parsing BCH_OPT_UINT Integral
2025-04-06 14:11 ` Kent Overstreet
2025-04-06 14:53   ` [PATCH v2] bcachefs: early return " Integral
2025-04-06 17:26     ` Kent Overstreet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox