]> git.kaiwu.me - njs.git/commitdiff
Fixed parseFloat().
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 28 Aug 2019 16:10:01 +0000 (19:10 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Wed, 28 Aug 2019 16:10:01 +0000 (19:10 +0300)
src/njs_number.h
src/njs_string.c
src/njs_strtod.c
src/test/njs_unit_test.c

index 16b75b2e04b8c37b29e4ae1978380b574cc75174..6317c31b507380104e10aa3ddc0a353730501d18 100644 (file)
@@ -139,7 +139,7 @@ njs_primitive_value_to_number(const njs_value_t *value)
         return njs_number(value);
     }
 
-    return njs_string_to_number(value, 1);
+    return njs_string_to_number(value, 0);
 }
 
 
index 6c8b769e00cf756019029e869b74d1ef31d6386e..848710f53c39920c7c2ef97f0092b83666bd1354 100644 (file)
@@ -3866,6 +3866,7 @@ njs_string_to_number(const njs_value_t *value, njs_bool_t parse_float)
 {
     double        num;
     size_t        size;
+    uint32_t      u;
     njs_bool_t    minus;
     const u_char  *p, *start, *end;
 
@@ -3884,15 +3885,17 @@ njs_string_to_number(const njs_value_t *value, njs_bool_t parse_float)
     end = p + size;
 
     while (p < end) {
-        if (*p != ' ' && *p != '\t') {
+        start = p;
+        u = njs_utf8_decode(&p, end);
+
+        if (!njs_utf8_is_whitespace(u)) {
+            p = start;
             break;
         }
-
-        p++;
     }
 
     if (p == end) {
-        return 0.0;
+        return parse_float ? NAN : 0.0;
     }
 
     minus = 0;
index 1cc3a924f8a9821db0ad357be634241b15645087..1efd91624a0cc665438c6d693b200a875023486d 100644 (file)
@@ -358,6 +358,10 @@ njs_strtod(const u_char **start, const u_char *end)
         }
     }
 
+    if (pos == data) {
+        return NAN;
+    }
+
     e = p + 1;
 
     if (e < end && (*p == 'e' || *p == 'E')) {
index 64e316eb90add7493a1538df6b21757173c1e540..cc8566c68de45022271e9b7106131e002b406150 100644 (file)
@@ -547,6 +547,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("5 - '\t 0x2 \t'"),
       njs_str("3") },
 
+    { njs_str("5 - '\t\\u000c0x2 \t'"),
+      njs_str("3") },
+
     { njs_str("5 - '0x2 z'"),
       njs_str("NaN") },
 
@@ -4152,6 +4155,10 @@ static njs_unit_test_t  njs_test[] =
                  "Array.prototype.slice.call(Array.prototype.fill.call(o, 1))"),
       njs_str("1,1") },
 
+    { njs_str("var o = {}; Object.defineProperty(o, 'length', {get:()=>'0x0002'}); "
+                 "Array.prototype.slice.call(Array.prototype.fill.call(o, 1))"),
+      njs_str("1,1") },
+
     { njs_str("var o = {}; Object.defineProperty(o, 'length', {get:()=> {throw TypeError('Boom')}}); "
                  "Array.prototype.fill.call(o, 1)"),
       njs_str("TypeError: Boom") },
@@ -12366,6 +12373,21 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("parseFloat('12345abc')"),
       njs_str("12345") },
 
+    { njs_str("parseFloat('')"),
+      njs_str("NaN") },
+
+    { njs_str("parseFloat('     \t')"),
+      njs_str("NaN") },
+
+    { njs_str("parseFloat('\\u20281')"),
+      njs_str("1") },
+
+    { njs_str("parseFloat('e11')"),
+      njs_str("NaN") },
+
+    { njs_str("parseFloat({toString(){return '  1'}})"),
+      njs_str("1") },
+
     { njs_str("parseFloat('1e2147483647')"),
       njs_str("Infinity") },