[PATCH weston v3 3/5] option-parser: Improve error checking for strtol call

Bryce Harrington bryce at osg.samsung.com
Fri Jul 29 16:43:58 UTC 2016


Make the error checking consistent with other strtol() calls.

Note that since strtol(nptr, &endptr) sets endptr == nptr if there were
no digits, this catches the case where the string was blank, so there's
no need to test *value != '\0'.

Signed-off-by: Bryce Harrington <bryce at osg.samsung.com>
---
 shared/option-parser.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/shared/option-parser.c b/shared/option-parser.c
index 33355b8..fb4a342 100644
--- a/shared/option-parser.c
+++ b/shared/option-parser.c
@@ -30,6 +30,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <errno.h>
 
 #include "config-parser.h"
 
@@ -40,11 +41,17 @@ handle_option(const struct weston_option *option, char *value)
 
 	switch (option->type) {
 	case WESTON_OPTION_INTEGER:
+		errno = 0;
 		* (int32_t *) option->data = strtol(value, &p, 10);
-		return *value && !*p;
+		if (errno != 0 || p == value || *p != '\0')
+			return 0;
+		return 1;
 	case WESTON_OPTION_UNSIGNED_INTEGER:
+		errno = 0;
 		* (uint32_t *) option->data = strtoul(value, &p, 10);
-		return *value && !*p;
+		if (errno != 0 || p == value || *p != '\0')
+			return 0;
+		return 1;
 	case WESTON_OPTION_STRING:
 		* (char **) option->data = strdup(value);
 		return 1;
-- 
1.9.1



More information about the wayland-devel mailing list