From: Christian Brauner Date: Wed, 18 Oct 2017 17:53:17 +0000 (+0200) Subject: utils: add lxc_safe_long_long() X-Git-Tag: lxc-2.0.10~617 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=76f40d1f95996b83d3e0dd9721bef017a7510d82;p=thirdparty%2Flxc.git utils: add lxc_safe_long_long() Signed-off-by: Christian Brauner --- diff --git a/src/lxc/utils.c b/src/lxc/utils.c index 0c188341a..f6b6584e2 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -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) { diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 1fe61ec39..315d87768 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -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);