tech-userlevel archive

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

[PATCH] strto*l() nonstandard behaviour fix



strto*l() functions should operate with bases from 2 till 36 and 0 as well for
special cases. Further information is provided in the ISO/IEC 9899:1999.
---
 common/lib/libc/stdlib/strtoll.c  |    9 +++++++++
 common/lib/libc/stdlib/strtoul.c  |    9 +++++++++
 common/lib/libc/stdlib/strtoull.c |    9 +++++++++
 lib/libc/stdlib/strtol.c          |    9 +++++++++
 4 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/common/lib/libc/stdlib/strtoll.c b/common/lib/libc/stdlib/strtoll.c
index a34b6f8..a6e23c4 100644
--- a/common/lib/libc/stdlib/strtoll.c
+++ b/common/lib/libc/stdlib/strtoll.c
@@ -122,6 +122,15 @@ strtoll(nptr, endptr, base)
                base = c == '0' ? 8 : 10;
 
        /*
+        * Check base range. We only accept bases from 2 to 36 as
+        * described in ISO/IEC 9899:1999 7.20.1.4.
+        */
+       if (base < 2 || base > 36) {
+               errno = EINVAL;
+               return 0;
+       }
+
+       /*
         * Compute the cutoff value between legal numbers and illegal
         * numbers.  That is the largest legal value, divided by the
         * base.  An input number that is greater than this value, if
diff --git a/common/lib/libc/stdlib/strtoul.c b/common/lib/libc/stdlib/strtoul.c
index e9b671c..fb34317 100644
--- a/common/lib/libc/stdlib/strtoul.c
+++ b/common/lib/libc/stdlib/strtoul.c
@@ -97,6 +97,15 @@ strtoul(nptr, endptr, base)
        if (base == 0)
                base = c == '0' ? 8 : 10;
 
+       /*
+        * Check base range. We only accept bases from 2 to 36 as
+        * described in ISO/IEC 9899:1999 7.20.1.4.
+        */
+       if (base < 2 || base > 36) {
+               errno = EINVAL;
+               return 0;
+       }
+
        cutoff = ULONG_MAX / (unsigned long)base;
        cutlim = (int)(ULONG_MAX % (unsigned long)base);
        for (acc = 0, any = 0;; c = (unsigned char) *s++) {
diff --git a/common/lib/libc/stdlib/strtoull.c 
b/common/lib/libc/stdlib/strtoull.c
index 6873c51..b4e8e69 100644
--- a/common/lib/libc/stdlib/strtoull.c
+++ b/common/lib/libc/stdlib/strtoull.c
@@ -105,6 +105,15 @@ strtoull(nptr, endptr, base)
        if (base == 0)
                base = c == '0' ? 8 : 10;
 
+       /*
+        * Check base range. We only accept bases from 2 to 36 as
+        * described in ISO/IEC 9899:1999 7.20.1.4.
+        */
+       if (base < 2 || base > 36) {
+               errno = EINVAL;
+               return 0;
+       }
+
        /* LONGLONG */
        cutoff = ULLONG_MAX / (unsigned long long int)base;
        /* LONGLONG */
diff --git a/lib/libc/stdlib/strtol.c b/lib/libc/stdlib/strtol.c
index cdadf5c..0a27351 100644
--- a/lib/libc/stdlib/strtol.c
+++ b/lib/libc/stdlib/strtol.c
@@ -92,6 +92,15 @@ strtol(nptr, endptr, base)
                base = c == '0' ? 8 : 10;
 
        /*
+        * Check base range. We only accept bases from 2 to 36 as
+        * described in ISO/IEC 9899:1999 7.20.1.4.
+        */
+       if (base < 2 || base > 36) {
+               errno = EINVAL;
+               return 0;
+       }
+
+       /*
         * Compute the cutoff value between legal numbers and illegal
         * numbers.  That is the largest legal value, divided by the
         * base.  An input number that is greater than this value, if
-- 
1.5.2.5



Home | Main Index | Thread Index | Old Index