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

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