From: Dmitry Volyntsev Date: Wed, 28 Aug 2019 16:10:01 +0000 (+0300) Subject: Fixed parseFloat(). X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=479c973d05344fed2f2040fd7902e13f55b0e937;p=njs.git Fixed parseFloat(). --- diff --git a/src/njs_number.h b/src/njs_number.h index 16b75b2e..6317c31b 100644 --- a/src/njs_number.h +++ b/src/njs_number.h @@ -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); } diff --git a/src/njs_string.c b/src/njs_string.c index 6c8b769e..848710f5 100644 --- a/src/njs_string.c +++ b/src/njs_string.c @@ -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; diff --git a/src/njs_strtod.c b/src/njs_strtod.c index 1cc3a924..1efd9162 100644 --- a/src/njs_strtod.c +++ b/src/njs_strtod.c @@ -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')) { diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 64e316eb..cc8566c6 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -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") },