Skip to content

Commit 91af0fd

Browse files
committed
Fix count variable calculation in typedarray copyWithin
Fixes #3130 JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi [email protected]
1 parent 97a0feb commit 91af0fd

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
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_create_error_reference_from_context ();
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_create_error_reference_from_context ();
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/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,16 +1887,16 @@ ecma_builtin_typedarray_prototype_copy_within (ecma_value_t this_arg, /**< this
18871887
}
18881888
}
18891889

1890-
int32_t distance = (int32_t) (end - start);
1891-
int32_t offset = (int32_t) (length - target);
1892-
int32_t count = JERRY_MIN (distance, offset);
1893-
18941890
if (target >= length || end == 0 || start >= end)
18951891
{
18961892
return ecma_copy_value (this_arg);
18971893
}
18981894
else
18991895
{
1896+
uint32_t distance = end - start;
1897+
uint32_t offset = length - target;
1898+
uint32_t count = JERRY_MIN (distance, offset);
1899+
19001900
memmove (typedarray_buffer_p + (target * element_size),
19011901
typedarray_buffer_p + (start * element_size),
19021902
(size_t) (count * element_size));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* extend_part
4141
* data buffer
4242
*
43-
* @return ecma_object_t *
43+
* @return ecma_object_t
4444
*/
4545
ecma_object_t *
4646
ecma_arraybuffer_new_object (ecma_length_t length) /**< length of the arraybuffer */
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)