mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] commands: test: support signed comparisons
@ 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 ` [PATCH 1/2] commands: test: support signed comparisons Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2023-09-13 11:59 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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

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




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

* [PATCH 2/2] test: self: add some test cases for test command
  2023-09-13 11:59 [PATCH 1/2] commands: test: support signed comparisons Ahmad Fatoum
@ 2023-09-13 11:59 ` Ahmad Fatoum
  2023-09-14  8:10 ` [PATCH 1/2] commands: test: support signed comparisons Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2023-09-13 11:59 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

With the rework to how arithmetic comparisons are conducted in the test
command, some tests are in order.

Prior to the rework, following test cases failed:

  test_test_command:42: [ -1 -le  1 ]: assertion failure, ret=1
  test_test_command:45: [ -9223372036854775808 -lt 9223372036854775807 ]: assertion failure, ret=1
  test_test_command:46: [ -9223372036854775808 -gt 9223372036854775807 ]: assertion failure, ret=0

But now they all succeed.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 test/self/Kconfig        |  4 +++
 test/self/Makefile       |  1 +
 test/self/test_command.c | 64 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+)
 create mode 100644 test/self/test_command.c

diff --git a/test/self/Kconfig b/test/self/Kconfig
index a4176ab8ffd6..15e00f0244b5 100644
--- a/test/self/Kconfig
+++ b/test/self/Kconfig
@@ -97,4 +97,8 @@ config SELFTEST_REGULATOR
 	depends on REGULATOR && OFDEVICE
 	select OF_OVERLAY
 
+config SELFTEST_TEST_COMMAND
+	bool "test command selftest"
+	depends on CMD_TEST
+
 endif
diff --git a/test/self/Makefile b/test/self/Makefile
index 78a337810d1f..8168abf26278 100644
--- a/test/self/Makefile
+++ b/test/self/Makefile
@@ -14,6 +14,7 @@ obj-$(CONFIG_SELFTEST_MMU) += mmu.o
 obj-$(CONFIG_SELFTEST_STRING) += string.o
 obj-$(CONFIG_SELFTEST_SETJMP) += setjmp.o
 obj-$(CONFIG_SELFTEST_REGULATOR) += regulator.o test_regulator.dtbo.o
+obj-$(CONFIG_SELFTEST_TEST_COMMAND) += test_command.o
 
 clean-files := *.dtb *.dtb.S .*.dtc .*.pre .*.dts *.dtb.z
 clean-files += *.dtbo *.dtbo.S .*.dtso
diff --git a/test/self/test_command.c b/test/self/test_command.c
new file mode 100644
index 000000000000..7a26f724f3ce
--- /dev/null
+++ b/test/self/test_command.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <bselftest.h>
+#include <command.h>
+
+BSELFTEST_GLOBALS();
+
+#if __LONG_MAX__ == 0x7ffffffff
+#define LONG_MIN_STR		"-2147483648"
+#define LONG_MIN_PLUS_1_STR	"-2147483647"
+#define LONG_MAX_STR		 "2147483647"
+#define LONG_MAX_PLUS_1_STR	 "2147483648"
+#else
+#define LONG_MIN_STR		"-9223372036854775808"
+#define LONG_MIN_PLUS_1_STR	"-9223372036854775807"
+#define LONG_MAX_STR		 "9223372036854775807"
+#define LONG_MAX_PLUS_1_STR	 "9223372036854775808"
+#endif
+
+static void __assert_eq(const char *expr, bool result, const char *func, int line)
+{
+	int ret;
+
+	total_tests++;
+
+	ret = run_command(expr);
+	if ((result && ret != 0) || (!result && ret != 1)) {
+		failed_tests++;
+		printf("%s:%d: %s: assertion failure, ret=%d\n", func, line, expr, ret);
+	}
+}
+
+#define assert_eq(expr, result) __assert_eq(expr, result, __func__, __LINE__)
+
+static void test_test_command(void)
+{
+	assert_eq("[  0 -eq  0 ]", true);
+	assert_eq("[  0 -eq  1 ]", false);
+	assert_eq("[ -1 -le  1 ]", true);
+	assert_eq("[ -1 -ge -5 ]", true);
+
+	assert_eq("[ " LONG_MIN_STR " -lt " LONG_MAX_STR " ]", true);
+	assert_eq("[ " LONG_MIN_STR " -gt " LONG_MAX_STR " ]", false);
+
+	assert_eq("[ " LONG_MIN_STR " -eq " LONG_MIN_STR " ]", true);
+	assert_eq("[ " LONG_MAX_STR " -eq " LONG_MAX_STR " ]", true);
+	assert_eq("[ " LONG_MIN_STR " -ne " LONG_MIN_STR " ]", false);
+	assert_eq("[ " LONG_MAX_STR " -ne " LONG_MAX_STR " ]", false);
+
+	assert_eq("[ " LONG_MIN_PLUS_1_STR " -eq " LONG_MIN_PLUS_1_STR " ]", true);
+	assert_eq("[ " LONG_MAX_PLUS_1_STR " -eq " LONG_MAX_PLUS_1_STR " ]", true);
+	assert_eq("[ " LONG_MIN_PLUS_1_STR " -ne " LONG_MIN_PLUS_1_STR " ]", false);
+	assert_eq("[ " LONG_MAX_PLUS_1_STR " -ne " LONG_MAX_PLUS_1_STR " ]", false);
+
+	assert_eq("[ " LONG_MIN_PLUS_1_STR " -gt " LONG_MIN_STR " ]", true);
+	assert_eq("[ " LONG_MAX_PLUS_1_STR " -gt " LONG_MAX_STR " ]", true);
+	assert_eq("[ " LONG_MIN_PLUS_1_STR " -lt " LONG_MIN_STR " ]", false);
+	assert_eq("[ " LONG_MAX_PLUS_1_STR " -lt " LONG_MAX_STR " ]", false);
+	assert_eq("[ " LONG_MIN_STR " -le " LONG_MIN_PLUS_1_STR " ]", true);
+	assert_eq("[ " LONG_MAX_STR " -le " LONG_MAX_PLUS_1_STR " ]", true);
+}
+bselftest(core, test_test_command);
-- 
2.39.2




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

* Re: [PATCH 1/2] commands: test: support signed comparisons
  2023-09-13 11:59 [PATCH 1/2] commands: test: support signed comparisons 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
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2023-09-14  8:10 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

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 |



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

end of thread, other threads:[~2023-09-14  8:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-13 11:59 [PATCH 1/2] commands: test: support signed comparisons 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 ` [PATCH 1/2] commands: test: support signed comparisons Sascha Hauer

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