Skip to content

Commit 90d206d

Browse files
authored
Fix free of NULL value in function ecma_typedarray_helper_dispatch_construct (#4473)
Currently, ecma_op_get_prototype_from_constructor may return NULL and the function didn't raise that exception. Also optimize multiple assignment of prototype_obj_p and multiple access of JERRY_CONTEXT (current_new_target) out. This fixes #4463 JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo luoyonggang@gmail.com
1 parent f894a8f commit 90d206d

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-helpers.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,20 @@ ecma_typedarray_helper_dispatch_construct (const ecma_value_t *arguments_list_p,
4040
{
4141
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
4242
ecma_builtin_id_t proto_id = ecma_typedarray_helper_get_prototype_id (typedarray_id);
43-
ecma_object_t *prototype_obj_p = ecma_builtin_get (proto_id);
43+
ecma_object_t *prototype_obj_p = NULL;
44+
ecma_object_t *current_new_target_p = JERRY_CONTEXT (current_new_target_p);
4445

45-
if (JERRY_CONTEXT (current_new_target_p))
46+
if (current_new_target_p != NULL)
4647
{
47-
prototype_obj_p = ecma_op_get_prototype_from_constructor (JERRY_CONTEXT (current_new_target_p), proto_id);
48+
prototype_obj_p = ecma_op_get_prototype_from_constructor (current_new_target_p, proto_id);
49+
if (prototype_obj_p == NULL)
50+
{
51+
return ECMA_VALUE_ERROR;
52+
}
53+
}
54+
else
55+
{
56+
prototype_obj_p = ecma_builtin_get (proto_id);
4857
}
4958

5059
ecma_value_t val = ecma_op_create_typedarray (arguments_list_p,
@@ -53,7 +62,7 @@ ecma_typedarray_helper_dispatch_construct (const ecma_value_t *arguments_list_p,
5362
ecma_typedarray_helper_get_shift_size (typedarray_id),
5463
typedarray_id);
5564

56-
if (JERRY_CONTEXT (current_new_target_p))
65+
if (current_new_target_p != NULL)
5766
{
5867
ecma_deref_object (prototype_obj_p);
5968
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
function Test262Error(message) {
16+
this.message = message || "";
17+
}
18+
19+
Test262Error.prototype.toString = function () {
20+
return "Test262Error: " + this.message;
21+
};
22+
23+
var newTarget = function () {}.bind(null);
24+
Object.defineProperty(newTarget, "prototype", {
25+
get() {
26+
throw new Test262Error();
27+
},
28+
});
29+
30+
var typedArrayConstructors = [
31+
Float64Array,
32+
Float32Array,
33+
Int32Array,
34+
Int16Array,
35+
Int8Array,
36+
Uint32Array,
37+
Uint16Array,
38+
Uint8Array,
39+
Uint8ClampedArray,
40+
];
41+
42+
for (var type of typedArrayConstructors) {
43+
try {
44+
Reflect.construct(Uint8ClampedArray, [], newTarget);
45+
} catch (error) {
46+
if (!(error instanceof Test262Error)) {
47+
throw "error must be instanceof Test262Error";
48+
}
49+
}
50+
}

tests/test262-esnext-excludelist.xml

-10
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,14 @@
198198
<test id="built-ins/TypedArray/prototype/toLocaleString/BigInt/get-length-uses-internal-arraylength.js"><reason></reason></test>
199199
<test id="built-ins/TypedArray/prototype/toLocaleString/BigInt/return-result.js"><reason></reason></test>
200200
<test id="built-ins/TypedArrayConstructors/ctors-bigint/buffer-arg/byteoffset-is-negative-zero.js"><reason></reason></test>
201-
<test id="built-ins/TypedArrayConstructors/ctors-bigint/buffer-arg/custom-proto-access-throws.js"><reason></reason></test>
202201
<test id="built-ins/TypedArrayConstructors/ctors-bigint/buffer-arg/defined-negative-length.js"><reason></reason></test>
203202
<test id="built-ins/TypedArrayConstructors/ctors-bigint/buffer-arg/toindex-byteoffset.js"><reason></reason></test>
204-
<test id="built-ins/TypedArrayConstructors/ctors-bigint/length-arg/custom-proto-access-throws.js"><reason></reason></test>
205203
<test id="built-ins/TypedArrayConstructors/ctors-bigint/length-arg/toindex-length.js"><reason></reason></test>
206-
<test id="built-ins/TypedArrayConstructors/ctors-bigint/no-args/custom-proto-access-throws.js"><reason></reason></test>
207-
<test id="built-ins/TypedArrayConstructors/ctors-bigint/object-arg/custom-proto-access-throws.js"><reason></reason></test>
208-
<test id="built-ins/TypedArrayConstructors/ctors-bigint/typedarray-arg/custom-proto-access-throws.js"><reason></reason></test>
209204
<test id="built-ins/TypedArrayConstructors/ctors/buffer-arg/byteoffset-is-negative-zero.js"><reason></reason></test>
210-
<test id="built-ins/TypedArrayConstructors/ctors/buffer-arg/custom-proto-access-throws.js"><reason></reason></test>
211205
<test id="built-ins/TypedArrayConstructors/ctors/buffer-arg/defined-negative-length.js"><reason></reason></test>
212206
<test id="built-ins/TypedArrayConstructors/ctors/buffer-arg/toindex-byteoffset.js"><reason></reason></test>
213-
<test id="built-ins/TypedArrayConstructors/ctors/length-arg/custom-proto-access-throws.js"><reason></reason></test>
214207
<test id="built-ins/TypedArrayConstructors/ctors/length-arg/toindex-length.js"><reason></reason></test>
215-
<test id="built-ins/TypedArrayConstructors/ctors/no-args/custom-proto-access-throws.js"><reason></reason></test>
216-
<test id="built-ins/TypedArrayConstructors/ctors/object-arg/custom-proto-access-throws.js"><reason></reason></test>
217208
<test id="built-ins/TypedArrayConstructors/ctors/object-arg/returns.js"><reason></reason></test>
218-
<test id="built-ins/TypedArrayConstructors/ctors/typedarray-arg/custom-proto-access-throws.js"><reason></reason></test>
219209
<test id="built-ins/TypedArrayConstructors/from/BigInt/custom-ctor-returns-other-instance.js"><reason></reason></test>
220210
<test id="built-ins/TypedArrayConstructors/from/BigInt/custom-ctor.js"><reason></reason></test>
221211
<test id="built-ins/TypedArrayConstructors/from/BigInt/new-instance-using-custom-ctor.js"><reason></reason></test>

0 commit comments

Comments
 (0)