]> git.kaiwu.me - njs.git/commitdiff
Fixed parseInt() zero radix parsing.
authorAndrey Zelenkov <zelenkov@nginx.com>
Wed, 31 May 2017 17:25:44 +0000 (20:25 +0300)
committerAndrey Zelenkov <zelenkov@nginx.com>
Wed, 31 May 2017 17:25:44 +0000 (20:25 +0300)
njs/njs_number.c
njs/test/njs_unit_test.c

index 6223b76d317a329e909741f8d5d79a356e8529ae..1d19d37ef955fc7468e08da3e9a1c57464eb50bd 100644 (file)
@@ -733,19 +733,23 @@ njs_number_parse_int(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
         }
 
         test_prefix = (end - p > 1);
+        radix = 0;
 
         if (nargs > 2) {
             radix = args[2].data.u.number;
 
-            if (radix < 2 || radix > 36) {
-                goto done;
-            }
+            if (radix != 0) {
+                if (radix < 2 || radix > 36) {
+                    goto done;
+                }
 
-            if (radix != 16) {
-                test_prefix = 0;
+                if (radix != 16) {
+                    test_prefix = 0;
+                }
             }
+        }
 
-        } else {
+        if (radix == 0) {
             radix = 10;
         }
 
index b339d775f6b0c6b56ad26dbeaafa469ef70e597e..fe1888376f3f057f00bf27ce855dd84dbfe1cb1f 100644 (file)
@@ -7144,6 +7144,12 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("parseInt('12345abc')"),
       nxt_string("12345") },
 
+    { nxt_string("parseInt('123', 0)"),
+      nxt_string("123") },
+
+    { nxt_string("parseInt('0XaBc', 0)"),
+      nxt_string("2748") },
+
     { nxt_string("parseInt('1010', 2)"),
       nxt_string("10") },