]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
utils: add lxc_safe_long_long()
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 18 Oct 2017 17:53:17 +0000 (19:53 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Thu, 9 Nov 2017 00:05:49 +0000 (01:05 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/utils.c
src/lxc/utils.h

index 0c188341a4630c30f5cdc43efe0b3f7ecebe466f..f6b6584e27e6defcfe7cbe1c24af1a457f9cf8bb 100644 (file)
@@ -2061,6 +2061,26 @@ int lxc_safe_long(const char *numstr, long int *converted)
        return 0;
 }
 
+int lxc_safe_long_long(const char *numstr, long long int *converted)
+{
+       char *err = NULL;
+       signed long long int sli;
+
+       errno = 0;
+       sli = strtoll(numstr, &err, 0);
+       if (errno == ERANGE && (sli == LLONG_MAX || sli == LLONG_MIN))
+               return -ERANGE;
+
+       if (errno != 0 && sli == 0)
+               return -EINVAL;
+
+       if (err == numstr || *err != '\0')
+               return -EINVAL;
+
+       *converted = sli;
+       return 0;
+}
+
 int lxc_switch_uid_gid(uid_t uid, gid_t gid)
 {
        if (setgid(gid) < 0) {
index 1fe61ec3986b1be058460b198f1809572c8ecae4..315d87768f2c37782500dee01e6ae522950ec732 100644 (file)
@@ -407,10 +407,11 @@ int lxc_preserve_ns(const int pid, const char *ns);
 bool task_blocking_signal(pid_t pid, int signal);
 
 /* Helper functions to parse numbers. */
-int lxc_safe_uint(const char *numstr, unsigned int *converted);
-int lxc_safe_int(const char *numstr, int *converted);
-int lxc_safe_long(const char *numstr, long int *converted);
-int lxc_safe_ulong(const char *numstr, unsigned long *converted);
+extern int lxc_safe_uint(const char *numstr, unsigned int *converted);
+extern int lxc_safe_int(const char *numstr, int *converted);
+extern int lxc_safe_long(const char *numstr, long int *converted);
+extern int lxc_safe_long_long(const char *numstr, long long int *converted);
+extern int lxc_safe_ulong(const char *numstr, unsigned long *converted);
 
 /* Switch to a new uid and gid. */
 int lxc_switch_uid_gid(uid_t uid, gid_t gid);