From: Dmitry Volyntsev Date: Mon, 17 Sep 2018 15:47:00 +0000 (+0300) Subject: Fixed String.slice() for undefined arguments. X-Git-Tag: 0.2.4~1 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=2847eccc88ca8194996d2373f89dd8e01770fd6a;p=njs.git Fixed String.slice() for undefined arguments. --- diff --git a/njs/njs_string.c b/njs/njs_string.c index acb4d2fc..938b7512 100644 --- a/njs/njs_string.c +++ b/njs/njs_string.c @@ -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; } } diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index e504c487..f883a82f 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -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("") },