[lxc-devel] [lxc/master] utils: fix num parsing functions

brauner on Github lxc-bot at linuxcontainers.org
Thu May 11 18:09:26 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 413 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170511/b29c4db2/attachment.bin>
-------------- next part --------------
From 13a5145befbc6c5849f3ecf7d4f94bb67423f797 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Thu, 11 May 2017 20:08:32 +0200
Subject: [PATCH] utils: fix num parsing functions

Suggested-by: Benedikt Rosenkranz beluro at web.de
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/lxc/utils.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index 778d4da..8581125 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -23,6 +23,7 @@
 
 #include "config.h"
 
+#include <ctype.h>
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -1999,11 +2000,18 @@ int lxc_safe_uint(const char *numstr, unsigned int *converted)
 	unsigned long int uli;
 
 	errno = 0;
+
+	while (isspace(*numstr))
+		numstr++;
+
+	if (*numstr == '-')
+		return -EINVAL;
+
 	uli = strtoul(numstr, &err, 0);
-	if (errno > 0)
+	if (errno == ERANGE && uli == ULONG_MAX)
 		return -errno;
 
-	if (!err || err == numstr || *err != '\0')
+	if (err == numstr || *err != '\0')
 		return -EINVAL;
 
 	if (uli > UINT_MAX)
@@ -2020,13 +2028,16 @@ int lxc_safe_int(const char *numstr, int *converted)
 
 	errno = 0;
 	sli = strtol(numstr, &err, 0);
-	if (errno > 0)
+        if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN))
+                return -errno;
+
+	if (errno != 0 && sli == 0)
 		return -errno;
 
-	if (!err || err == numstr || *err != '\0')
+	if (err == numstr || *err != '\0')
 		return -EINVAL;
 
-	if (sli > INT_MAX)
+	if (sli > INT_MAX || sli < INT_MAX)
 		return -ERANGE;
 
 	*converted = (int)sli;
@@ -2040,15 +2051,15 @@ int lxc_safe_long(const char *numstr, long int *converted)
 
 	errno = 0;
 	sli = strtol(numstr, &err, 0);
-	if (errno > 0)
+        if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN))
+                return -errno;
+
+	if (errno != 0 && sli == 0)
 		return -errno;
 
-	if (!err || err == numstr || *err != '\0')
+	if (err == numstr || *err != '\0')
 		return -EINVAL;
 
-	if (sli > LONG_MAX)
-		return -ERANGE;
-
 	*converted = sli;
 	return 0;
 }


More information about the lxc-devel mailing list