Skip to content

Commit b6f174c

Browse files
committed
Gracefully throw an error when parser runs out of memory.
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 405bccf commit b6f174c

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

jerry-core/parser/js/js-parser-mem.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ parser_malloc (parser_context_t *context_p, /**< context */
3939
void *result;
4040

4141
JERRY_ASSERT (size > 0);
42-
result = jmem_heap_alloc_block (size);
43-
if (result == 0)
42+
result = jmem_heap_alloc_block_null_on_error (size);
43+
44+
if (result == NULL)
4445
{
4546
parser_raise_error (context_p, PARSER_ERR_OUT_OF_MEMORY);
4647
}

jerry-core/parser/js/js-parser.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,13 @@ parser_parse_script (const uint8_t *source_p, /**< source code */
22412241

22422242
if (!*bytecode_data_p)
22432243
{
2244+
if (parse_error.error == PARSER_ERR_OUT_OF_MEMORY)
2245+
{
2246+
/* It is unlikely that memory can be allocated in an out-of-memory
2247+
* situation. However, a simple value can still be thrown. */
2248+
return ecma_make_error_value (ecma_make_simple_value (ECMA_SIMPLE_VALUE_NULL));
2249+
}
2250+
22442251
return ecma_raise_syntax_error (parser_error_to_string (parse_error.error));
22452252
}
22462253

tests/jerry/parser-oom.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2016 Samsung Electronics Co., Ltd.
2+
// Copyright 2016 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
/* String which is 32 bytes long. */
17+
var str = "1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+";
18+
19+
for (var i = 0; i < 10; i++) {
20+
str = str + str;
21+
}
22+
23+
str = "(function() { return " + str + "1 })";
24+
25+
/* Eat memory. */
26+
var array = [];
27+
28+
try
29+
{
30+
for (var i = 0; i < 15; i++)
31+
{
32+
array[i] = eval(str);
33+
}
34+
assert (false);
35+
}
36+
catch (err)
37+
{
38+
array = null;
39+
assert (err === null);
40+
}

0 commit comments

Comments
 (0)