Tests: fix TAP_AUTOTIME parsing in tap.c
authorMichael Jeanson <mjeanson@efficios.com>
Thu, 29 Feb 2024 19:34:14 +0000 (14:34 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 29 Feb 2024 20:03:01 +0000 (15:03 -0500)
The atoi() function can return '0' on error and is not guaranteed to set
errno accordingly. Use strtol() instead which can also return 0 on error
but will set errno properly.

Check errno after the function call to distinguish between the value '0'
and the error code '0'.

Change-Id: I5a82bb0f18d7e398dc3594aede5a38e6fc10dd7b
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
tests/utils/tap/tap.c

index 1ef417fed34a70b4897918815c2c783db4840fd5..c92c14140e4404b5e506d3571e9029715b8863d2 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <assert.h>
 #include <ctype.h>
+#include <errno.h>
 #include <limits.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -183,11 +184,22 @@ void _tap_init(void)
                   with Test::Harness */
                setbuf(stdout, 0);
 
-               char *autotime_env = getenv("TAP_AUTOTIME");
-               if (autotime_env != NULL) {
-                       time_tests = atoi(autotime_env);
-                       if (time_tests != 0) {
-                               time_tests = 1;
+               /*
+                * Check if the TAP_AUTOTIME environment variable is set and
+                * contains at least one byte.
+                */
+               const char *autotime_env = getenv("TAP_AUTOTIME");
+               if (autotime_env != NULL && strnlen(autotime_env, 1)) {
+                       int tap_autotime;
+
+                       /*
+                        * Check if TAP_AUTOTIME is '0', also check errno
+                        * because strtol() can return '0' on error.
+                        */
+                       errno = 0;
+                       tap_autotime = strtol(autotime_env, NULL, 10);
+                       if (tap_autotime == 0 && errno == 0) {
+                               time_tests = 0;
                        }
                }
 
This page took 0.026028 seconds and 4 git commands to generate.