mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <sha@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH 1/2] commands: test: support signed comparisons
Date: Thu, 14 Sep 2023 10:10:03 +0200	[thread overview]
Message-ID: <20230914081003.GO637806@pengutronix.de> (raw)
In-Reply-To: <20230913115958.1858470-1-a.fatoum@pengutronix.de>

On Wed, Sep 13, 2023 at 01:59:57PM +0200, Ahmad Fatoum wrote:
> So far signed integers in binary comparisons were accepted, but compared
> unsigned resulting in [ "-1" -gt 0 ] returning true.
> 
> Rework this, so that:
> 
>   - If one side is signed, the comparison is signed
>   - If both sides are unsigned, the comparison is unsigned
> 
> The latter is not required by POSIX, but it ensures that we don't break
> users who so far did comparisons with integers > LONG_MAX without issue.
> 
> While at it, we start checking that the arguments in arithmetic
> comparisons are indeed integers. This has the result that e.g.
> [ "1x" -eq "1x" ] will no longer return success, but that aligns us with
> usual shell behavior and protects the users from silent truncation when
> a number > LONG_MAX is compared with a negative number.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  commands/test.c | 66 ++++++++++++++++++++++++++++++++++++++-----------
>  1 file changed, 51 insertions(+), 15 deletions(-)

Applied, thanks

Sascha

> 
> diff --git a/commands/test.c b/commands/test.c
> index 1130bf2e5bd6..13005b97deb3 100644
> --- a/commands/test.c
> +++ b/commands/test.c
> @@ -78,11 +78,56 @@ static int string_comp(const char *left_op, const char *right_op, bool bash_test
>  	return strcmp(left_op, right_op);
>  }
>  
> +static int parse_number(const char *str, long *num, bool signedcmp)
> +{
> +	int ret;
> +
> +	ret = signedcmp ? kstrtol(str, 0, num) : kstrtoul(str, 0, num);
> +	if (ret)
> +		printf("test: %s: integer expression expected\n", str);
> +
> +	return ret;
> +}
> +
> +#define __do_arith_cmp(x, op, y, signedcmp) \
> +	((signedcmp) ? (long)(x) op (long)(y) : (x) op (y))
> +
> +static int arith_comp(const char *a_str, const char *b_str, int op)
> +{
> +	ulong a, b;
> +	bool signedcmp = a_str[0] == '-' || b_str[0] == '-';
> +	int ret;
> +
> +	ret = parse_number(a_str, &a, signedcmp);
> +	if (ret)
> +		return ret;
> +
> +	ret = parse_number(b_str, &b, signedcmp);
> +	if (ret)
> +		return ret;
> +
> +	switch (op) {
> +	case OPT_ARITH_EQUAL:
> +		return __do_arith_cmp(a, ==, b, signedcmp);
> +	case OPT_ARITH_NOT_EQUAL:
> +		return __do_arith_cmp(a, !=, b, signedcmp);
> +	case OPT_ARITH_GREATER_EQUAL:
> +		return __do_arith_cmp(a, >=, b, signedcmp);
> +	case OPT_ARITH_GREATER_THAN:
> +		return __do_arith_cmp(a,  >, b, signedcmp);
> +	case OPT_ARITH_LESS_EQUAL:
> +		return __do_arith_cmp(a, <=, b, signedcmp);
> +	case OPT_ARITH_LESS_THAN:
> +		return __do_arith_cmp(a,  <, b, signedcmp);
> +	}
> +
> +	return -EINVAL;
> +}
> +
>  static int do_test(int argc, char *argv[])
>  {
>  	char **ap;
>  	int left, adv, expr, last_expr, neg, last_cmp, opt, zero;
> -	ulong a, b;
>  	struct stat statbuf;
>  	bool bash_test = false;
>  
> @@ -199,9 +244,8 @@ static int do_test(int argc, char *argv[])
>  			if (left < 3)
>  				break;
>  
> -			a = simple_strtol(ap[0], NULL, 0);
> -			b = simple_strtol(ap[2], NULL, 0);
> -			switch (parse_opt(ap[1])) {
> +			opt = parse_opt(ap[1]);
> +			switch (opt) {
>  			case OPT_EQUAL:
>  			case OPT_EQUAL_BASH:
>  				expr = string_comp(ap[0], ap[2], bash_test) == 0;
> @@ -210,22 +254,14 @@ static int do_test(int argc, char *argv[])
>  				expr = string_comp(ap[0], ap[2], bash_test) != 0;
>  				break;
>  			case OPT_ARITH_EQUAL:
> -				expr = a == b;
> -				break;
>  			case OPT_ARITH_NOT_EQUAL:
> -				expr = a != b;
> -				break;
>  			case OPT_ARITH_LESS_THAN:
> -				expr = a < b;
> -				break;
>  			case OPT_ARITH_LESS_EQUAL:
> -				expr = a <= b;
> -				break;
>  			case OPT_ARITH_GREATER_THAN:
> -				expr = a > b;
> -				break;
>  			case OPT_ARITH_GREATER_EQUAL:
> -				expr = a >= b;
> +				expr = arith_comp(ap[0], ap[2], opt);
> +				if (expr < 0)
> +					return 1;
>  				break;
>  			default:
>  				expr = 1;
> -- 
> 2.39.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



      parent reply	other threads:[~2023-09-14  8:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-13 11:59 Ahmad Fatoum
2023-09-13 11:59 ` [PATCH 2/2] test: self: add some test cases for test command Ahmad Fatoum
2023-09-14  8:10 ` Sascha Hauer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230914081003.GO637806@pengutronix.de \
    --to=sha@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox