[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] Fix nonzero detection in integer parsing
From: |
Jouke Witteveen |
Subject: |
[PATCH] Fix nonzero detection in integer parsing |
Date: |
Mon, 20 Dec 2021 20:24:10 +0100 |
* src/function.c (parse_textint): Fix nonzero detection.
* tests/scripts/functions/intcmp: Extend test coverage.
---
The `*p++ == '0'` test should have been `*p++ != '0'` and did not need
to be repeated for every digit anyway. By pure luck, all numbers used
in tests included a digit '0', so this mistake went unnoticed.
src/function.c | 6 +++---
tests/scripts/functions/intcmp | 11 +++++++----
2 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/src/function.c b/src/function.c
index c107a387..28594942 100644
--- a/src/function.c
+++ b/src/function.c
@@ -1293,7 +1293,7 @@ parse_textint (const char *number, const char *msg,
const char *after_sign, *after_number;
const char *p = next_token (number);
int negative = *p == '-';
- int nonzero = 0;
+ int nonzero;
if (*p == '\0')
OS (fatal, *expanding_var, _("%s: empty value"), msg);
@@ -1306,9 +1306,9 @@ parse_textint (const char *number, const char *msg,
*numstart = p;
while (ISDIGIT (*p))
- if (*p++ == '0')
- nonzero = 1;
+ p++;
after_number = p;
+ nonzero = *numstart != after_number;
*sign = negative ? -nonzero : nonzero;
/* Check for extra non-whitespace stuff after the value. */
diff --git a/tests/scripts/functions/intcmp b/tests/scripts/functions/intcmp
index 7466ff97..24e25b22 100644
--- a/tests/scripts/functions/intcmp
+++ b/tests/scripts/functions/intcmp
@@ -9,15 +9,18 @@ n = -10
# Zero
z = 0
# Positive
-p = 1000000000
+p = 888
min = -9223372036854775808
max = 9223372036854775807
huge = 8857889956778499040639527525992734031025567913257255490371761260681427
.RECIPEPREFIX = >
all:
> @echo 0_1 $(intcmp $n,$n)
-> @echo 0_2 $(intcmp $n,$z)
-> @echo 0_3 $(intcmp $z,$n)
+> @echo 0_2 $(intcmp $z,$z)
+> @echo 0_3 $(intcmp -$z,$z)
+> @echo 0_4 $(intcmp $p,$p)
+> @echo 0_5 $(intcmp $n,$z)
+> @echo 0_6 $(intcmp $z,$n)
> @echo 1_1 $(intcmp $n,$n,$(shell echo lt))
> @echo 1_2 $(intcmp $n,$z,$(shell echo lt))
> @echo 1_3 $(intcmp $z,$n,$(shell echo lt))
@@ -36,7 +39,7 @@ all:
> @echo 5_1 $(intcmp $(huge),-$(huge),lt,eq,gt)
> @echo 5_2 $(intcmp -$(huge),-$(huge),lt,eq,gt)
> @echo 5_3 $(intcmp +$(huge),$(huge),lt,eq,gt)
-', '', "0_1 -10\n0_2\n0_3\n1_1\n1_2 lt\n1_3\n2_1 lt\n2_2 ge\n2_3 ge\n3_0\n3_1
lt\n3_2 gt\n3_3 eq\n4_0 lt\n4_1 gt\n4_2 eq\n4_3 eq\n5_0 lt\n5_1 gt\n5_2 eq\n5_3
eq\n");
+', '', "0_1 -10\n0_2 0\n0_3 0\n0_4 888\n0_5\n0_6\n1_1\n1_2 lt\n1_3\n2_1
lt\n2_2 ge\n2_3 ge\n3_0\n3_1 lt\n3_2 gt\n3_3 eq\n4_0 lt\n4_1 gt\n4_2 eq\n4_3
eq\n5_0 lt\n5_1 gt\n5_2 eq\n5_3 eq\n");
# Test error conditions
--
2.34.1
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [PATCH] Fix nonzero detection in integer parsing,
Jouke Witteveen <=