Skip to content

Commit d0f7aea

Browse files
committed
Fix free of NULL value in function ecma_typedarray_helper_dispatch_construct
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 jerryscript-project#4463 JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo [email protected]
1 parent 91baa17 commit d0f7aea

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

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

+18-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "ecma-builtins.h"
2121
#include "ecma-gc.h"
2222
#include "ecma-objects.h"
23+
#include "ecma-exceptions.h"
2324
#include "ecma-typedarray-object.h"
2425
#include "ecma-function-object.h"
2526
#include "jcontext.h"
@@ -40,11 +41,24 @@ ecma_typedarray_helper_dispatch_construct (const ecma_value_t *arguments_list_p,
4041
{
4142
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
4243
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);
44+
ecma_object_t *current_new_target_p = JERRY_CONTEXT (current_new_target);
45+
ecma_object_t *prototype_obj_p;
4446

45-
if (JERRY_CONTEXT (current_new_target))
47+
if (current_new_target_p != NULL)
4648
{
47-
prototype_obj_p = ecma_op_get_prototype_from_constructor (JERRY_CONTEXT (current_new_target), proto_id);
49+
prototype_obj_p = ecma_op_get_prototype_from_constructor (current_new_target_p, proto_id);
50+
if (prototype_obj_p == NULL)
51+
{
52+
if (jcontext_has_pending_exception ())
53+
{
54+
return ECMA_VALUE_ERROR;
55+
}
56+
return ecma_raise_type_error (ECMA_ERR_MSG ("TypedArray constructor should have prototype"));
57+
}
58+
}
59+
else
60+
{
61+
prototype_obj_p = ecma_builtin_get (proto_id);
4862
}
4963

5064
ecma_value_t val = ecma_op_create_typedarray (arguments_list_p,
@@ -53,7 +67,7 @@ ecma_typedarray_helper_dispatch_construct (const ecma_value_t *arguments_list_p,
5367
ecma_typedarray_helper_get_shift_size (typedarray_id),
5468
typedarray_id);
5569

56-
if (JERRY_CONTEXT (current_new_target))
70+
if (current_new_target_p != NULL)
5771
{
5872
ecma_deref_object (prototype_obj_p);
5973
}
+50
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+
}

0 commit comments

Comments
 (0)