[njs] parseInt() fixed for hexadecimal literals.

Dmitry Volyntsev xeioex at nginx.com
Thu May 25 11:39:59 UTC 2017


details:   http://hg.nginx.org/njs/rev/fc6c8da00858
branches:  
changeset: 341:fc6c8da00858
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu May 25 14:16:31 2017 +0300
description:
parseInt() fixed for hexadecimal literals.

diffstat:

 njs/njs_number.c         |  33 +++++++++++++++++++--------------
 njs/test/njs_unit_test.c |  21 +++++++++++++++++++++
 2 files changed, 40 insertions(+), 14 deletions(-)

diffs (87 lines):

diff -r 96fda9957427 -r fc6c8da00858 njs/njs_number.c
--- a/njs/njs_number.c	Wed Apr 19 17:48:56 2017 +0300
+++ b/njs/njs_number.c	Thu May 25 14:16:31 2017 +0300
@@ -662,7 +662,7 @@ njs_number_parse_int(njs_vm_t *vm, njs_v
     u_char             *p, *end;
     int64_t            n;
     uint8_t            radix;
-    nxt_bool_t         minus;
+    nxt_bool_t         minus, test_prefix;
     njs_string_prop_t  string;
 
     num = NAN;
@@ -693,25 +693,30 @@ njs_number_parse_int(njs_vm_t *vm, njs_v
             p++;
         }
 
-        if (end - p > 1 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
-            p += 2;
-            radix = 16;
+        test_prefix = (end - p > 1);
+
+        if (nargs > 2) {
+            radix = args[2].data.u.number;
+
+            if (radix < 2 || radix > 36) {
+                goto done;
+            }
+
+            if (radix != 16) {
+                test_prefix = 0;
+            }
 
         } else {
             radix = 10;
         }
 
-        if (nargs > 2) {
-            n = args[2].data.u.number;
-
-            if (n != 0) {
+        if (test_prefix && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
+            p += 2;
+            radix = 16;
+        }
 
-                if (n < 2 || n > 36) {
-                    goto done;
-                }
-
-                radix = n;
-            }
+        if (p == end) {
+            goto done;
         }
 
         n = njs_number_radix_parse(p, end, radix, 0);
diff -r 96fda9957427 -r fc6c8da00858 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Wed Apr 19 17:48:56 2017 +0300
+++ b/njs/test/njs_unit_test.c	Thu May 25 14:16:31 2017 +0300
@@ -7047,6 +7047,27 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("parseInt('njscript', 36)"),
       nxt_string("1845449130881") },
 
+    { nxt_string("parseInt('0x')"),
+      nxt_string("NaN") },
+
+    { nxt_string("parseInt('0x', 10)"),
+      nxt_string("0") },
+
+    { nxt_string("parseInt('0x', 16)"),
+      nxt_string("NaN") },
+
+    { nxt_string("parseInt('0x', 33)"),
+      nxt_string("0") },
+
+    { nxt_string("parseInt('0x', 34)"),
+      nxt_string("33") },
+
+    { nxt_string("parseInt('0', 1)"),
+      nxt_string("NaN") },
+
+    { nxt_string("parseInt('0', 37)"),
+      nxt_string("NaN") },
+
     { nxt_string("parseFloat('12345abc')"),
       nxt_string("12345") },
 


More information about the nginx-devel mailing list