option-parser: Improve error checking for strtol call

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@osg.samsung.com>
Reviewed-by: Eric Engestrom <eric.engestrom@imgtec.com>
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Bryce Harrington 2016-08-03 17:40:50 -07:00
parent 139fcabe7c
commit d9779e3ff1
1 changed files with 9 additions and 2 deletions

View File

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