Browse Source

Fix a bug in stdlib.h.

Giovanni Mascellani 5 years ago
parent
commit
8cf89d3b4d
3 changed files with 14 additions and 0 deletions
  1. 1 0
      asmg/c_test.g
  2. 4 0
      diskfs/stdlib/_strtox.h
  3. 9 0
      diskfs/tests/test_stdlib.c

+ 1 - 0
asmg/c_test.g

@@ -189,6 +189,7 @@ fun c_run_testcases 0 {
   tests "/disk1/tests/test_stdlib.c" "test_free_null" 1 "" c_run_testcase ;
   tests "/disk1/tests/test_stdlib.c" "test_realloc_null_free" 1 "" c_run_testcase ;
   tests "/disk1/tests/test_stdlib.c" "test_qsort" 1 "" c_run_testcase ;
+  tests "/disk1/tests/test_stdlib.c" "test_strtoull_zero" 1 "" c_run_testcase ;
 
   tests "/disk1/tests/test_string.c" "test_strcmp1" 1 "" c_run_testcase ;
   tests "/disk1/tests/test_string.c" "test_strcmp2" 1 "" c_run_testcase ;

+ 4 - 0
diskfs/stdlib/_strtox.h

@@ -58,6 +58,10 @@ const char * _strtox_prelim( const char * p, char * sign, int * base )
         else if ( *base == 0 )
         {
             *base = 8;
+            /* Rewind one character back, so that the string composed
+               of just a zero is correctly parsed (and endptr is set
+               appropriately). */
+            --p;
         }
         else
         {

+ 9 - 0
diskfs/tests/test_stdlib.c

@@ -86,3 +86,12 @@ int test_qsort() {
     if ( strcmp( s, presort ) != 0 ) return 0;
     return 1;
 }
+
+int test_strtoull_zero() {
+    char *p = "0";
+    char *q;
+    unsigned long long n = strtoull(p, &q, 0);
+    if (*q != '\0') return 0;
+    if (n != 0) return 0;
+    return 1;
+}