Welcome! Log In Create A New Profile

Advanced

[njs] Fixed Array.prototype.join() with "undefined" argument.

Alexander Borisov
October 30, 2019 09:44AM
details: https://hg.nginx.org/njs/rev/758bbc9f105a
branches:
changeset: 1208:758bbc9f105a
user: Alexander Borisov <alexander.borisov@nginx.com>
date: Wed Oct 30 16:43:12 2019 +0300
description:
Fixed Array.prototype.join() with "undefined" argument.

This closes #232 issue on GitHub.

diffstat:

src/njs_array.c | 26 +++++++++++++-------------
src/test/njs_unit_test.c | 3 +++
2 files changed, 16 insertions(+), 13 deletions(-)

diffs (59 lines):

diff -r 2f21b596ce15 -r 758bbc9f105a src/njs_array.c
--- a/src/njs_array.c Wed Oct 30 16:43:10 2019 +0300
+++ b/src/njs_array.c Wed Oct 30 16:43:12 2019 +0300
@@ -1144,13 +1144,22 @@ njs_array_prototype_join(njs_vm_t *vm, n
return ret;
}

- if (nargs > 1 && !njs_is_string(&args[1])) {
- ret = njs_value_to_string(vm, &args[1], &args[1]);
- if (njs_slow_path(ret != NJS_OK)) {
- return ret;
+ value = njs_arg(args, nargs, 1);
+
+ if (njs_slow_path(!njs_is_string(value))) {
+ if (njs_is_undefined(value)) {
+ value = njs_value_arg(&njs_string_comma);
+
+ } else {
+ ret = njs_value_to_string(vm, value, value);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return ret;
+ }
}
}

+ (void) njs_string_prop(&separator, value);
+
if (!njs_is_array(&args[0]) || njs_array_len(&args[0]) == 0) {
vm->retval = njs_string_empty;
return NJS_OK;
@@ -1232,15 +1241,6 @@ njs_array_prototype_join(njs_vm_t *vm, n
}
}

- if (nargs > 1) {
- value = &args[1];
-
- } else {
- value = njs_value_arg(&njs_string_comma);
- }
-
- (void) njs_string_prop(&separator, value);
-
size += separator.size * (array->length - 1);
length += separator.length * (array->length - 1);

diff -r 2f21b596ce15 -r 758bbc9f105a src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Wed Oct 30 16:43:10 2019 +0300
+++ b/src/test/njs_unit_test.c Wed Oct 30 16:43:12 2019 +0300
@@ -7756,6 +7756,9 @@ static njs_unit_test_t njs_test[] =
{ njs_str("[].join.call()"),
njs_str("TypeError: cannot convert null or undefined to object") },

+ { njs_str("[1,2,3].join(undefined)"),
+ njs_str("1,2,3") },
+
{ njs_str("[].slice.call()"),
njs_str("TypeError: cannot convert null or undefined to object") },

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Fixed Array.prototype.join() with "undefined" argument.

Alexander Borisov 61 October 30, 2019 09:44AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 78
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready