[njs] Fixed String.slice() for undefined arguments.

Dmitry Volyntsev xeioex at nginx.com
Mon Sep 17 15:48:12 UTC 2018


details:   http://hg.nginx.org/njs/rev/b00d4680635a
branches:  
changeset: 606:b00d4680635a
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Sep 17 18:47:00 2018 +0300
description:
Fixed String.slice() for undefined arguments.

diffstat:

 njs/njs_string.c         |  65 ++++++++++++++++++++++++-----------------------
 njs/test/njs_unit_test.c |  12 ++++++++
 2 files changed, 45 insertions(+), 32 deletions(-)

diffs (107 lines):

diff -r 85f57cc123c2 -r b00d4680635a njs/njs_string.c
--- a/njs/njs_string.c	Fri Sep 14 14:19:03 2018 +0300
+++ b/njs/njs_string.c	Mon Sep 17 18:47:00 2018 +0300
@@ -1236,48 +1236,49 @@ static nxt_noinline void
 njs_string_slice_args(njs_slice_prop_t *slice, njs_value_t *args,
     nxt_uint_t nargs)
 {
-    ssize_t  start, end, length;
+    ssize_t            start, end, length;
+    const njs_value_t  *value;
 
     length = slice->string_length;
-    start = 0;
-
-    if (nargs > 1) {
-        start = args[1].data.u.number;
+
+    value = njs_arg(args, nargs, 1);
+    start = value->data.u.number;
+
+    if (start < 0) {
+        start += length;
 
         if (start < 0) {
-            start += length;
-
-            if (start < 0) {
-                start = 0;
-            }
+            start = 0;
         }
-
-        if (start >= length) {
-            start = 0;
-            length = 0;
+    }
+
+    if (start >= length) {
+        start = 0;
+        length = 0;
+
+    } else {
+        if (!njs_is_void(njs_arg(args, nargs, 2))) {
+            value = njs_arg(args, nargs, 2);
+            end = value->data.u.number;
 
         } else {
             end = length;
-
-            if (nargs > 2) {
-                end = args[2].data.u.number;
-
-                if (end < 0) {
-                    end += length;
-                }
+        }
+
+        if (end < 0) {
+            end += length;
+        }
+
+        if (length >= end) {
+            length = end - start;
+
+            if (length < 0) {
+                start = 0;
+                length = 0;
             }
 
-            if (length >= end) {
-                length = end - start;
-
-                if (length < 0) {
-                    start = 0;
-                    length = 0;
-                }
-
-            } else {
-                length -= start;
-            }
+        } else {
+            length -= start;
         }
     }
 
diff -r 85f57cc123c2 -r b00d4680635a njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Fri Sep 14 14:19:03 2018 +0300
+++ b/njs/test/njs_unit_test.c	Mon Sep 17 18:47:00 2018 +0300
@@ -3794,6 +3794,18 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("'abcdefgh'.slice(3)"),
       nxt_string("defgh") },
 
+    { nxt_string("'abcdefgh'.slice(undefined, undefined)"),
+      nxt_string("abcdefgh") },
+
+    { nxt_string("'abcdefgh'.slice(undefined)"),
+      nxt_string("abcdefgh") },
+
+    { nxt_string("'abcdefgh'.slice(undefined, 1)"),
+      nxt_string("a") },
+
+    { nxt_string("'abcdefgh'.slice(3, undefined)"),
+      nxt_string("defgh") },
+
     { nxt_string("'abcde'.slice(50)"),
       nxt_string("") },
 


More information about the nginx-devel mailing list