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

[tyndur-devel] [PATCH] strto(u)l: 0x ist nur ein Praefix, wenn danach noch was kommt



! strtou(l): Wenn nach dem 0x keine gueltige Ziffer mehr kommt, ist es
  entweder keine Hexzahl (Basis 0) oder eine Null ohne 0x vorne dran
  (Basis 16).

Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
---
 src/lib/stdlibc/strtol.c |    9 ++++++++-
 src/lib/string.c         |   15 ++++++++++++---
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/lib/stdlibc/strtol.c b/src/lib/stdlibc/strtol.c
index d605144..2387e98 100644
--- a/src/lib/stdlibc/strtol.c
+++ b/src/lib/stdlibc/strtol.c
@@ -31,6 +31,13 @@
 #include "limits.h"
 #include "types.h"
 
+static int is_hexdigit(char x)
+{
+    return (x >= '0' && x <= '9')
+        || (x >= 'a' && x <= 'f')
+        || (x >= 'A' && x <= 'F');
+}
+
 long strtol(const char *str, char **endptr, int base)
 {
     long retval = 0;
@@ -56,7 +63,7 @@ long strtol(const char *str, char **endptr, int base)
                 base = 16;
             }
 
-            if (base == 16) {
+            if (base == 16 && is_hexdigit(str[2])) {
                 str += 2;
             }
         } else if (base == 0) {
diff --git a/src/lib/string.c b/src/lib/string.c
index 3125c2c..ae809a6 100644
--- a/src/lib/string.c
+++ b/src/lib/string.c
@@ -65,6 +65,13 @@ void itoa(unsigned int n, char *s, unsigned int base)
 	s[y] = '\0';
 }
 
+static int is_hexdigit(char x)
+{
+    return (x >= '0' && x <= '9')
+        || (x >= 'a' && x <= 'f')
+        || (x >= 'A' && x <= 'F');
+}
+
 unsigned long long int strtoull(const char *nptr, char **endptr, int base)
 {
     unsigned long long int result = 0;
@@ -94,8 +101,10 @@ unsigned long long int strtoull(const char *nptr, char **endptr, int base)
             base = 10;
             if (*nptr == '0') {
                 if (*++nptr == 'x') {
-                    base = 16;
-                    nptr++;
+                    if (is_hexdigit(nptr[1])) {
+                        base = 16;
+                        nptr++;
+                    }
                 } else {
                     base = 8;
                 }
@@ -103,7 +112,7 @@ unsigned long long int strtoull(const char *nptr, char **endptr, int base)
             break;
 
         case 16:
-            if ((*nptr == '0') && (*(nptr+1) == 'x')) {
+            if ((*nptr == '0') && (nptr[1] == 'x') && is_hexdigit(nptr[2])) {
                 nptr += 2;
             }
             break;
-- 
1.6.0.2