Skip to content

Commit d5d248d

Browse files
committed
Limit arraybuffer allocation size
Fixes #3130 Previously we could allocate more than INT32_MAX size for the arraybuffer, but thats a wrong behaviour, now we limited this size. JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi [email protected]
1 parent 97a0feb commit d5d248d

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

jerry-core/api/jerry.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3073,7 +3073,14 @@ jerry_create_arraybuffer (const jerry_length_t size) /**< size of the ArrayBuffe
30733073
jerry_assert_api_available ();
30743074

30753075
#if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
3076-
return jerry_return (ecma_make_object_value (ecma_arraybuffer_new_object (size)));
3076+
ecma_object_t *arraybuffer_obj_p = ecma_arraybuffer_new_object (size);
3077+
3078+
if (JERRY_UNLIKELY (arraybuffer_obj_p == NULL))
3079+
{
3080+
return ECMA_VALUE_ERROR;
3081+
}
3082+
3083+
return jerry_return (ecma_make_object_value (arraybuffer_obj_p));
30773084
#else /* !ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
30783085
JERRY_UNUSED (size);
30793086
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer not supported.")));
@@ -3107,6 +3114,12 @@ jerry_create_arraybuffer_external (const jerry_length_t size, /**< size of the b
31073114
ecma_object_t *arraybuffer = ecma_arraybuffer_new_object_external (size,
31083115
buffer_p,
31093116
(ecma_object_native_free_callback_t) free_cb);
3117+
3118+
if (JERRY_UNLIKELY (arraybuffer == NULL))
3119+
{
3120+
return ECMA_VALUE_ERROR;
3121+
}
3122+
31103123
return jerry_return (ecma_make_object_value (arraybuffer));
31113124
#else /* !ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) */
31123125
JERRY_UNUSED (size);

jerry-core/ecma/builtin-objects/ecma-builtin-arraybuffer-prototype.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ ecma_builtin_arraybuffer_prototype_object_slice (ecma_value_t this_arg, /**< thi
129129
JERRY_ASSERT (start <= len && end <= len);
130130
ecma_length_t new_len = (end >= start) ? (end - start) : 0;
131131
ecma_object_t *new_arraybuffer_p = ecma_arraybuffer_new_object (new_len);
132+
133+
if (JERRY_UNLIKELY (new_arraybuffer_p == NULL))
134+
{
135+
return ECMA_VALUE_ERROR;
136+
}
137+
132138
lit_utf8_byte_t *old_buf = ecma_arraybuffer_get_buffer (object_p);
133139
lit_utf8_byte_t *new_buf = ecma_arraybuffer_get_buffer (new_arraybuffer_p);
134140

jerry-core/ecma/operations/ecma-arraybuffer-object.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
ecma_object_t *
4646
ecma_arraybuffer_new_object (ecma_length_t length) /**< length of the arraybuffer */
4747
{
48+
if (length >= INT32_MAX)
49+
{
50+
ecma_raise_range_error (ECMA_ERR_MSG ("Array buffer allocation failed"));
51+
return NULL;
52+
}
53+
4854
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_ARRAYBUFFER_PROTOTYPE);
4955
ecma_object_t *object_p = ecma_create_object (prototype_obj_p,
5056
sizeof (ecma_extended_object_t) + length,
@@ -76,6 +82,12 @@ ecma_arraybuffer_new_object_external (ecma_length_t length, /**< length of the b
7682
void *buffer_p, /**< pointer for ArrayBuffer's buffer backing */
7783
ecma_object_native_free_callback_t free_cb) /**< buffer free callback */
7884
{
85+
if (length >= INT32_MAX)
86+
{
87+
ecma_raise_range_error (ECMA_ERR_MSG ("Array buffer allocation failed"));
88+
return NULL;
89+
}
90+
7991
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_ARRAYBUFFER_PROTOTYPE);
8092
ecma_object_t *object_p = ecma_create_object (prototype_obj_p,
8193
sizeof (ecma_arraybuffer_external_info),
@@ -146,7 +158,14 @@ ecma_op_create_arraybuffer_object (const ecma_value_t *arguments_list_p, /**< li
146158

147159
uint32_t length_uint32 = ecma_number_to_uint32 (length_num);
148160

149-
return ecma_make_object_value (ecma_arraybuffer_new_object (length_uint32));
161+
ecma_object_t *arraybuffer_obj_p = ecma_arraybuffer_new_object (length_uint32);
162+
163+
if (JERRY_UNLIKELY (arraybuffer_obj_p == NULL))
164+
{
165+
return ECMA_VALUE_ERROR;
166+
}
167+
168+
return ecma_make_object_value (arraybuffer_obj_p);
150169
} /* ecma_op_create_arraybuffer_object */
151170

152171
/**

jerry-core/ecma/operations/ecma-typedarray-object.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,15 @@ ecma_typedarray_create_object_with_length (ecma_length_t array_length, /**< leng
243243
return ecma_raise_range_error (ECMA_ERR_MSG ("Maximum typedarray size is reached."));
244244
}
245245

246-
ecma_value_t new_arraybuffer_p = ecma_make_object_value (ecma_arraybuffer_new_object (byte_length));
246+
ecma_object_t *arraybuffer_obj_p = ecma_arraybuffer_new_object (byte_length);
247+
248+
if (JERRY_UNLIKELY (arraybuffer_obj_p == NULL))
249+
{
250+
return ECMA_VALUE_ERROR;
251+
}
252+
253+
ecma_value_t new_arraybuffer_p = ecma_make_object_value (arraybuffer_obj_p);
254+
247255
ecma_object_t *object_p = ecma_create_object (proto_p,
248256
sizeof (ecma_extended_object_t),
249257
ECMA_OBJECT_TYPE_PSEUDO_ARRAY);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
var arrb = new ArrayBuffer(2147483648);
16+
var arr = new Uint8Array(arrb);
17+
18+
try {
19+
arr.copyWithin({}, 8);
20+
assert(false);
21+
} catch (e) {
22+
assert (e instanceof RangeError);
23+
}

0 commit comments

Comments
 (0)