Commit 6460fc55 by Christian Brauner Committed by Stéphane Graber

utils: add lxc_safe_int()

parent 3d5bac92
...@@ -2008,3 +2008,23 @@ int lxc_safe_uint(const char *numstr, unsigned int *converted) ...@@ -2008,3 +2008,23 @@ int lxc_safe_uint(const char *numstr, unsigned int *converted)
*converted = (unsigned)uli; *converted = (unsigned)uli;
return 0; return 0;
} }
int lxc_safe_int(const char *numstr, int *converted)
{
char *err = NULL;
signed long int sli;
errno = 0;
sli = strtol(numstr, &err, 0);
if (errno > 0)
return -errno;
if (!err || err == numstr || *err != '\0')
return -EINVAL;
if (sli > INT_MAX)
return -ERANGE;
*converted = (int)sli;
return 0;
}
...@@ -319,5 +319,6 @@ bool task_blocking_signal(pid_t pid, int signal); ...@@ -319,5 +319,6 @@ bool task_blocking_signal(pid_t pid, int signal);
/* Helper functions to parse numbers. */ /* Helper functions to parse numbers. */
int lxc_safe_uint(const char *numstr, unsigned int *converted); int lxc_safe_uint(const char *numstr, unsigned int *converted);
int lxc_safe_int(const char *numstr, int *converted);
#endif /* __LXC_UTILS_H */ #endif /* __LXC_UTILS_H */
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