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

Zhao Gongyi zhaogongyi@huawei.com
Mon Dec 5 07:54:32 CET 2022


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

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;
 	}

--
2.17.1



More information about the ltp mailing list