Commit d7cfb1de by Christian Brauner Committed by Stéphane Graber

utils: add lxc_safe_uint()

This function safely parses an unsigned integer. On success it returns 0 and stores the unsigned integer in @converted. On error it returns a negative errno. Signed-off-by: 's avatarChristian Brauner <christian.brauner@canonical.com>
parent 9af8795f
......@@ -1988,3 +1988,23 @@ int lxc_preserve_ns(const int pid, const char *ns)
return open(path, O_RDONLY | O_CLOEXEC);
}
int lxc_safe_uint(const char *numstr, unsigned int *converted)
{
char *err = NULL;
unsigned long int uli;
errno = 0;
uli = strtoul(numstr, &err, 0);
if (errno > 0)
return -errno;
if (!err || err == numstr || *err != '\0')
return -EINVAL;
if (uli > UINT_MAX)
return -ERANGE;
*converted = (unsigned)uli;
return 0;
}
......@@ -316,4 +316,8 @@ int lxc_preserve_ns(const int pid, const char *ns);
/* Check whether a signal is blocked by a process. */
bool task_blocking_signal(pid_t pid, int signal);
/* Helper functions to parse numbers. */
int lxc_safe_uint(const char *numstr, unsigned int *converted);
#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