[njs] Fixed parseFloat().
Dmitry Volyntsev
xeioex at nginx.com
Wed Aug 28 16:10:24 UTC 2019
details: https://hg.nginx.org/njs/rev/719c79b0f07d
branches:
changeset: 1146:719c79b0f07d
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Wed Aug 28 19:10:01 2019 +0300
description:
Fixed parseFloat().
diffstat:
src/njs_number.h | 2 +-
src/njs_string.c | 11 +++++++----
src/njs_strtod.c | 4 ++++
src/test/njs_unit_test.c | 22 ++++++++++++++++++++++
4 files changed, 34 insertions(+), 5 deletions(-)
diffs (105 lines):
diff -r 949a244b6b2c -r 719c79b0f07d src/njs_number.h
--- a/src/njs_number.h Tue Aug 27 18:58:43 2019 +0300
+++ b/src/njs_number.h Wed Aug 28 19:10:01 2019 +0300
@@ -139,7 +139,7 @@ njs_primitive_value_to_number(const njs_
return njs_number(value);
}
- return njs_string_to_number(value, 1);
+ return njs_string_to_number(value, 0);
}
diff -r 949a244b6b2c -r 719c79b0f07d src/njs_string.c
--- a/src/njs_string.c Tue Aug 27 18:58:43 2019 +0300
+++ b/src/njs_string.c Wed Aug 28 19:10:01 2019 +0300
@@ -3866,6 +3866,7 @@ njs_string_to_number(const njs_value_t *
{
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 *
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 -r 949a244b6b2c -r 719c79b0f07d src/njs_strtod.c
--- a/src/njs_strtod.c Tue Aug 27 18:58:43 2019 +0300
+++ b/src/njs_strtod.c Wed Aug 28 19:10:01 2019 +0300
@@ -358,6 +358,10 @@ njs_strtod(const u_char **start, const u
}
}
+ if (pos == data) {
+ return NAN;
+ }
+
e = p + 1;
if (e < end && (*p == 'e' || *p == 'E')) {
diff -r 949a244b6b2c -r 719c79b0f07d src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Tue Aug 27 18:58:43 2019 +0300
+++ b/src/test/njs_unit_test.c Wed Aug 28 19:10:01 2019 +0300
@@ -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") },
More information about the nginx-devel
mailing list