Patchwork [3/6] Add more error handling to strtosz()

login
register
mail settings
Submitter Jes Sorensen
Date Oct. 7, 2010, 3:01 p.m.
Message ID <1286463710-28262-4-git-send-email-Jes.Sorensen@redhat.com>
Download mbox | patch
Permalink /patch/67065/
State New
Headers show

Comments

Jes Sorensen - Oct. 7, 2010, 3:01 p.m.
From: Jes Sorensen <Jes.Sorensen@redhat.com>

Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
---
 cutils.c |   10 +++++++---
 1 files changed, 7 insertions(+), 3 deletions(-)
Paolo Bonzini - Oct. 7, 2010, 3:52 p.m.
On 10/07/2010 05:01 PM, Jes.Sorensen@redhat.com wrote:
> +    if (tmpval > ~(size_t)0)

Since -1 is an error, this needs to be >=.

Paolo

Patch

diff --git a/cutils.c b/cutils.c
index 0782032..012eb11 100644
--- a/cutils.c
+++ b/cutils.c
@@ -292,6 +292,7 @@  int fcntl_setfl(int fd, int flag)
 ssize_t strtosz(const char *nptr, char **end)
 {
     ssize_t retval = -1;
+    int64_t tmpval;
     char *endptr;
     int mul_required = 0;
     double val, mul = 1;
@@ -301,9 +302,9 @@  ssize_t strtosz(const char *nptr, char **end)
         mul_required = 1;
     }
 
+    errno = 0;
     val = strtod(nptr, &endptr);
-
-    if (val < 0)
+    if (endptr == nptr || errno != 0 || val < 0)
         goto fail;
 
     switch (*endptr++) {
@@ -332,7 +333,10 @@  ssize_t strtosz(const char *nptr, char **end)
         goto fail;
     }
 
-    retval = (ssize_t)(val * mul);
+    tmpval = (val * mul);
+    if (tmpval > ~(size_t)0)
+        goto fail;
+    retval = tmpval;
 
     if (end)
         *end = endptr;