[njs] Fixed parseInt() zero radix parsing.

Andrey Zelenkov zelenkov at nginx.com
Wed May 31 17:26:39 UTC 2017


details:   http://hg.nginx.org/njs/rev/b592f24c9ac6
branches:  
changeset: 348:b592f24c9ac6
user:      Andrey Zelenkov <zelenkov at nginx.com>
date:      Wed May 31 20:25:44 2017 +0300
description:
Fixed parseInt() zero radix parsing.

diffstat:

 njs/njs_number.c         |  16 ++++++++++------
 njs/test/njs_unit_test.c |   6 ++++++
 2 files changed, 16 insertions(+), 6 deletions(-)

diffs (49 lines):

diff -r a38c33e9f728 -r b592f24c9ac6 njs/njs_number.c
--- a/njs/njs_number.c	Tue May 30 19:35:08 2017 +0300
+++ b/njs/njs_number.c	Wed May 31 20:25:44 2017 +0300
@@ -733,19 +733,23 @@ njs_number_parse_int(njs_vm_t *vm, njs_v
         }
 
         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;
         }
 
diff -r a38c33e9f728 -r b592f24c9ac6 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue May 30 19:35:08 2017 +0300
+++ b/njs/test/njs_unit_test.c	Wed May 31 20:25:44 2017 +0300
@@ -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") },
 


More information about the nginx-devel mailing list