Re: [PATCH 1/2] commands: test: support signed comparisons

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxx>
> ---
>  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 |




[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux