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

login
register
mail settings
Submitter Jes Sorensen
Date Oct. 8, 2010, 9:15 a.m.
Message ID <1286529360-5715-4-git-send-email-Jes.Sorensen@redhat.com>
Download mbox | patch
Permalink /patch/67141/
State New
Headers show

Comments

Jes Sorensen - Oct. 8, 2010, 9:15 a.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(-)
Stefan Weil - Oct. 8, 2010, 9:38 a.m.
Am 08.10.2010 11:15, schrieb Jes.Sorensen@redhat.com:
> 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(-)
>
> diff --git a/cutils.c b/cutils.c
> index 0782032..e5a135e 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;
>    
See CODING_STYLE.
>
>       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;
>    
See CODING_STYLE.
> +    retval = tmpval;
>
>       if (end)
>           *end = endptr;
>

Patch

diff --git a/cutils.c b/cutils.c
index 0782032..e5a135e 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;