[njs] Fixed Date.prototype.toISOString() with invalid date values.

Dmitry Volyntsev xeioex at nginx.com
Mon Feb 15 16:26:53 UTC 2021


details:   https://hg.nginx.org/njs/rev/6c37b561e923
branches:  
changeset: 1603:6c37b561e923
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Feb 15 15:25:00 2021 +0000
description:
Fixed Date.prototype.toISOString() with invalid date values.

This fixes #373 issue on Github.

diffstat:

 src/njs_date.c           |  11 ++++++++++-
 src/test/njs_unit_test.c |   9 +++++++++
 2 files changed, 19 insertions(+), 1 deletions(-)

diffs (61 lines):

diff -r 374dec48b9e4 -r 6c37b561e923 src/njs_date.c
--- a/src/njs_date.c	Thu Feb 11 14:39:13 2021 +0000
+++ b/src/njs_date.c	Mon Feb 15 15:25:00 2021 +0000
@@ -1136,6 +1136,8 @@ static njs_int_t
 njs_date_prototype_to_string(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t fmt)
 {
+    double  time;
+
     if (njs_slow_path(!njs_is_date(&args[0]))) {
         njs_type_error(vm, "cannot convert %s to date",
                        njs_type_string(args[0].type));
@@ -1143,7 +1145,14 @@ njs_date_prototype_to_string(njs_vm_t *v
         return NJS_ERROR;
     }
 
-    return njs_date_string(vm, &vm->retval, fmt, njs_date(&args[0])->time);
+    time = njs_date(&args[0])->time;
+
+    if (fmt == NJS_DATE_FMT_TO_ISO_STRING && isnan(time)) {
+        njs_range_error(vm, "Invalid time value");
+        return NJS_ERROR;
+    }
+
+    return njs_date_string(vm, &vm->retval, fmt, time);
 }
 
 
diff -r 374dec48b9e4 -r 6c37b561e923 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Thu Feb 11 14:39:13 2021 +0000
+++ b/src/test/njs_unit_test.c	Mon Feb 15 15:25:00 2021 +0000
@@ -14431,6 +14431,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("var d = new Date(2011, 5, 24, 18, 45); d.toDateString()"),
       njs_str("Fri Jun 24 2011") },
 
+    { njs_str("new Date(NaN).toDateString()"),
+      njs_str("Invalid Date") },
+
     { njs_str("var d = new Date(2011, 5, 24, 18, 45);"
                  "d.toTimeString().slice(0, 17)"),
       njs_str("18:45:00 GMT+0000") },
@@ -14477,6 +14480,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("(new Date('-012345-07-01T00:00Z')).toUTCString()"),
       njs_str("Thu, 01 Jul -12345 00:00:00 GMT") },
 
+    { njs_str("new Date(NaN).toUTCString()"),
+      njs_str("Invalid Date") },
+
     { njs_str("var d = new Date(-62167219200000); d.toISOString()"),
       njs_str("0000-01-01T00:00:00.000Z") },
 
@@ -14485,6 +14491,9 @@ static njs_unit_test_t  njs_test[] =
 
     { njs_str("var d = new Date(-62198755200000); d.toISOString()"),
       njs_str("-000001-01-01T00:00:00.000Z") },
+
+    { njs_str("new Date(NaN).toISOString()"),
+      njs_str("RangeError: Invalid time value") },
 #endif
 
     { njs_str("Date.UTC(2011, 5, 24, 6, 0)"),


More information about the nginx-devel mailing list