Skip to content

Commit 91818be

Browse files
rerobikadbatyai
authored andcommitted
Limit the constructed string size in String.prototype.repeat (#3066)
This patch fixes #3063. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent 1b84a17 commit 91818be

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

jerry-core/ecma/base/ecma-globals.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,11 @@ typedef enum
13481348
#define ECMA_GET_DIRECT_STRING_VALUE(string_p) \
13491349
(((uintptr_t) (string_p)) >> ECMA_DIRECT_STRING_SHIFT)
13501350

1351+
/**
1352+
* Maximum number of bytes that a long-utf8-string is able to store
1353+
*/
1354+
#define ECMA_STRING_SIZE_LIMIT UINT32_MAX
1355+
13511356
typedef enum
13521357
{
13531358
ECMA_STRING_CONTAINER_HEAP_UTF8_STRING, /**< actual data is on the heap as an utf-8 (cesu8) string

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,12 @@ ecma_builtin_string_prototype_object_repeat (ecma_string_t *original_string_p, /
19131913
}
19141914

19151915
lit_utf8_size_t size = ecma_string_get_utf8_size (original_string_p);
1916+
1917+
if ((uint32_t) length >= (ECMA_STRING_SIZE_LIMIT / size))
1918+
{
1919+
return ecma_raise_range_error (ECMA_ERR_MSG ("Invalid string length"));
1920+
}
1921+
19161922
lit_utf8_size_t total_size = size * (lit_utf8_size_t) length;
19171923

19181924
JMEM_DEFINE_LOCAL_ARRAY (str_buffer, total_size, lit_utf8_byte_t);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 str = "123" + "test123";
16+
17+
try {
18+
str.repeat([1073741823]);
19+
assert(false);
20+
} catch (e) {
21+
assert (e instanceof RangeError);
22+
}

0 commit comments

Comments
 (0)