utils: fix num parsing functions

Suggested-by: Benedikt Rosenkranz beluro@web.de Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent bd3dfa33
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "config.h" #include "config.h"
#include <ctype.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
...@@ -1998,12 +1999,18 @@ int lxc_safe_uint(const char *numstr, unsigned int *converted) ...@@ -1998,12 +1999,18 @@ int lxc_safe_uint(const char *numstr, unsigned int *converted)
char *err = NULL; char *err = NULL;
unsigned long int uli; unsigned long int uli;
while (isspace(*numstr))
numstr++;
if (*numstr == '-')
return -EINVAL;
errno = 0; errno = 0;
uli = strtoul(numstr, &err, 0); uli = strtoul(numstr, &err, 0);
if (errno > 0) if (errno == ERANGE && uli == ULONG_MAX)
return -errno; return -errno;
if (!err || err == numstr || *err != '\0') if (err == numstr || *err != '\0')
return -EINVAL; return -EINVAL;
if (uli > UINT_MAX) if (uli > UINT_MAX)
...@@ -2020,13 +2027,16 @@ int lxc_safe_int(const char *numstr, int *converted) ...@@ -2020,13 +2027,16 @@ int lxc_safe_int(const char *numstr, int *converted)
errno = 0; errno = 0;
sli = strtol(numstr, &err, 0); sli = strtol(numstr, &err, 0);
if (errno > 0) if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN))
return -errno;
if (errno != 0 && sli == 0)
return -errno; return -errno;
if (!err || err == numstr || *err != '\0') if (err == numstr || *err != '\0')
return -EINVAL; return -EINVAL;
if (sli > INT_MAX) if (sli > INT_MAX || sli < INT_MIN)
return -ERANGE; return -ERANGE;
*converted = (int)sli; *converted = (int)sli;
...@@ -2040,14 +2050,14 @@ int lxc_safe_long(const char *numstr, long int *converted) ...@@ -2040,14 +2050,14 @@ int lxc_safe_long(const char *numstr, long int *converted)
errno = 0; errno = 0;
sli = strtol(numstr, &err, 0); sli = strtol(numstr, &err, 0);
if (errno > 0) if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN))
return -errno; return -errno;
if (!err || err == numstr || *err != '\0') if (errno != 0 && sli == 0)
return -EINVAL; return -errno;
if (sli > LONG_MAX) if (err == numstr || *err != '\0')
return -ERANGE; return -EINVAL;
*converted = sli; *converted = sli;
return 0; return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment