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: Christian Brauner <christian.brauner@canonical.com>
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;
+}
/* 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 */