]> git.kaiwu.me - njs.git/commitdiff
Fixed String.slice() for undefined arguments.
authorDmitry Volyntsev <xeioex@nginx.com>
Mon, 17 Sep 2018 15:47:00 +0000 (18:47 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Mon, 17 Sep 2018 15:47:00 +0000 (18:47 +0300)
njs/njs_string.c
njs/test/njs_unit_test.c

index acb4d2fcb5f61dcf07ae46834b235d325d90b7a8..938b7512fc37f04acef97e7285b92b293f1005c0 100644 (file)
@@ -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;
-            }
+        if (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 (length >= end) {
-                length = end - start;
+        if (end < 0) {
+            end += length;
+        }
 
-                if (length < 0) {
-                    start = 0;
-                    length = 0;
-                }
+        if (length >= end) {
+            length = end - start;
 
-            } else {
-                length -= start;
+            if (length < 0) {
+                start = 0;
+                length = 0;
             }
+
+        } else {
+            length -= start;
         }
     }
 
index e504c487ac727b62022e7c3d52159c123e8c62da..f883a82f2af0494a1123d5ee054a4b9375f7154f 100644 (file)
@@ -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("") },