[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT PATCH 1/2] lib/nolibc: strto* returns ERANGE/EINVAL error codes



In accordance with the posix documentation, strto* functions shall
report ERANGE if the correct value is outside the range of
representable values and EINVAL if no conversion was performed

Signed-off-by: Sharan Santhanam <sharan.santhanam@xxxxxxxxx>
---
 lib/nolibc/stdlib.c | 41 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 34 insertions(+), 7 deletions(-)

diff --git a/lib/nolibc/stdlib.c b/lib/nolibc/stdlib.c
index d3c94e1..76236af 100644
--- a/lib/nolibc/stdlib.c
+++ b/lib/nolibc/stdlib.c
@@ -72,6 +72,13 @@ unsigned long strtoul(const char *nptr, char **endptr, int 
base)
        unsigned long cutoff;
        int neg = 0, any, cutlim;
 
+       if (base < 0 || base == 1 || base > 36) {
+               errno = -EINVAL;
+               any = 0;
+               acc = 0;
+               goto exit;
+       }
+
        /*
         * See strtol for comments as to the logic used.
         */
@@ -111,10 +118,12 @@ unsigned long strtoul(const char *nptr, char **endptr, 
int base)
                        acc += c;
                }
        }
-       if (any < 0)
+       if (any < 0) {
                acc = ULONG_MAX;
-       else if (neg)
+               errno = ERANGE;
+       } else if (neg)
                acc = -acc;
+exit:
        if (endptr != 0)
                *endptr = __DECONST(char *, any ? s - 1 : nptr);
        return acc;
@@ -134,12 +143,18 @@ long long strtoll(const char *nptr, char **endptr, int 
base)
        unsigned long long qbase, cutoff;
        int neg, any, cutlim;
 
+       s = nptr;
+       if (base < 0 || base == 1 || base > 36) {
+               errno = -EINVAL;
+               any = 0;
+               acc = 0;
+               goto exit;
+       }
        /*
         * Skip white space and pick up leading +/- sign if any.
         * If base is 0, allow 0x for hex and 0 for octal, else
         * assume decimal; if base is already 16, allow 0x.
         */
-       s = nptr;
        do {
                c = *s++;
        } while (isspace(c));
@@ -203,10 +218,13 @@ long long strtoll(const char *nptr, char **endptr, int 
base)
                        acc += c;
                }
        }
-       if (any < 0)
+       if (any < 0) {
+               errno = ERANGE;
                acc = neg ? LLONG_MIN : LLONG_MAX;
-       else if (neg)
+       } else if (neg)
                acc = -acc;
+
+exit:
        if (endptr != 0)
                *endptr = __DECONST(char *, any ? s - 1 : nptr);
        return acc;
@@ -226,6 +244,12 @@ unsigned long long strtoull(const char *nptr, char 
**endptr, int base)
        unsigned long long qbase, cutoff;
        int neg, any, cutlim;
 
+       if (base < 0 || base == 1 || base > 36) {
+               errno = -EINVAL;
+               any = 0;
+               acc = 0;
+               goto exit;
+       }
        /*
         * See strtoq for comments as to the logic used.
         */
@@ -269,10 +293,13 @@ unsigned long long strtoull(const char *nptr, char 
**endptr, int base)
                        acc += c;
                }
        }
-       if (any < 0)
+       if (any < 0) {
+               errno = ERANGE;
                acc = ULLONG_MAX;
-       else if (neg)
+       } else if (neg)
                acc = -acc;
+
+exit:
        if (endptr != 0)
                *endptr = __DECONST(char *, any ? s - 1 : nptr);
        return acc;
-- 
2.7.4


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.