[njs] Fixed njs_string_to_number() with additional space symbols.

Dmitry Volyntsev xeioex at nginx.com
Sat Mar 4 02:50:12 UTC 2023


details:   https://hg.nginx.org/njs/rev/9367f3dfe0d9
branches:  
changeset: 2064:9367f3dfe0d9
user:      Artem S. Povalyukhin <artem.povaluhin at gmail.com>
date:      Fri Mar 03 22:57:30 2023 +0300
description:
Fixed njs_string_to_number() with additional space symbols.

This closes #621 issue on Github.

diffstat:

 src/njs_string.c         |  2 +-
 src/test/njs_unit_test.c |  3 +++
 2 files changed, 4 insertions(+), 1 deletions(-)

diffs (25 lines):

diff -r 7ad9dd5da3fb -r 9367f3dfe0d9 src/njs_string.c
--- a/src/njs_string.c	Fri Mar 03 17:49:11 2023 -0800
+++ b/src/njs_string.c	Fri Mar 03 22:57:30 2023 +0300
@@ -3974,7 +3974,7 @@ njs_string_to_number(const njs_value_t *
 
     if (!parse_float) {
         while (p < end) {
-            if (*p != ' ' && *p != '\t') {
+            if (!njs_is_whitespace(*p)) {
                 return NAN;
             }
 
diff -r 7ad9dd5da3fb -r 9367f3dfe0d9 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri Mar 03 17:49:11 2023 -0800
+++ b/src/test/njs_unit_test.c	Fri Mar 03 22:57:30 2023 +0300
@@ -13339,6 +13339,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("Number('123')"),
       njs_str("123") },
 
+    { njs_str("['1', ' 1 ', '1\\t', '1\\n', '1\\r\\n'].reduce((a, x) => a + Number(x), 0)"),
+      njs_str("5") },
+
     { njs_str("Number('0.'+'1'.repeat(128))"),
       njs_str("0.1111111111111111") },
 


More information about the nginx-devel mailing list