diff mbox series

[v3,3/3] lib: Adjust the position of the checking of the return value

Message ID 20221205065432.242539-4-zhaogongyi@huawei.com
State Accepted
Headers show
Series Add handling of abnormal input for parse_opts() | expand

Commit Message

Zhao Gongyi Dec. 5, 2022, 6:54 a.m. UTC
we need to check the return value before the checking of the endptr,
otherwise, it will report out of range when calling of
SAFE_STRTOUL("a100", 1, 10000000):

  TBROK: strtoul(a100): 0 is out of range 1 - 10000000

and it is expected that reported as:

  TBROK: Invalid value: 'a100'

Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
---
 lib/safe_macros.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

--
2.17.1
diff mbox series

Patch

diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index 0fb5580ac..1ade829aa 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -614,16 +614,16 @@  unsigned long safe_strtoul(const char *file, const int lineno,
 		return rval;
 	}

-	if (rval > max || rval < min) {
+	if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
-			"strtoul(%s): %lu is out of range %lu - %lu",
-			str, rval, min, max);
+			"Invalid value: '%s'", str);
 		return 0;
 	}

-	if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
+	if (rval > max || rval < min) {
 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
-			"Invalid value: '%s'", str);
+			"strtoul(%s): %lu is out of range %lu - %lu",
+			str, rval, min, max);
 		return 0;
 	}