config-parser: Improve error checks for strtol/strtoul calls
Check errno, which is set of over/underflow, out of range, etc. Also check for empty strings (the usages covered in this patch already also cover the case where there are non-digits present). Set errno to 0 before making the strto*l call in case of pre-existing errors (i.e. ENOTTY when running under the testsuite). This follows the error checking style used in Wayland (c.f. wayland-client.c and scanner.c). In tests, also check errno, and add testcases for parsing '0'. Signed-off-by: Bryce Harrington <bryce@osg.samsung.com> Reviewed-by: Eric Engestrom <eric.engestrom@imgtec.com>
This commit is contained in:
@@ -169,8 +169,9 @@ weston_config_section_get_int(struct weston_config_section *section,
|
||||
return -1;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
*value = strtol(entry->value, &end, 0);
|
||||
if (*end != '\0') {
|
||||
if (errno != 0 || end == entry->value || *end != '\0') {
|
||||
*value = default_value;
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
@@ -195,8 +196,9 @@ weston_config_section_get_uint(struct weston_config_section *section,
|
||||
return -1;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
*value = strtoul(entry->value, &end, 0);
|
||||
if (*end != '\0') {
|
||||
if (errno != 0 || end == entry->value || *end != '\0') {
|
||||
*value = default_value;
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user