Skip to content

Commit 8359bed

Browse files
committed
Merge pull request #1572 from pguyot/w11/fix-make_tuple_2
Fix possible memory corruption in `erlang:make_tuple/2` These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 9c30f60 + 42f36d2 commit 8359bed

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ memory error
5858
- Fixed possible concurrency problems in ESP32 UART driver
5959
- Fixed concurrency and memory leak related to links and monitors
6060
- Fixed issues with parsing of line references for stack traces
61+
- Fixed memory corruption issue with `erlang:make_tuple/2`
6162

6263
### Changed
6364

src/libAtomVM/nifs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,7 @@ static term nif_erlang_make_tuple_2(Context *ctx, int argc, term argv[])
16581658
RAISE_ERROR(BADARG_ATOM);
16591659
}
16601660

1661-
if (UNLIKELY(memory_ensure_free_opt(ctx, count_elem + 1, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
1661+
if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(count_elem), 1, argv + 1, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
16621662
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
16631663
}
16641664
term new_tuple = term_alloc_tuple(count_elem, &ctx->heap);

tests/erlang_tests/test_make_tuple.erl

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@
2020

2121
-module(test_make_tuple).
2222

23-
-export([start/0, f/1, g/1]).
23+
-export([start/0, f/1, g/2]).
2424

2525
start() ->
26-
g(f(2)) + g(f(0)).
26+
{} = ?MODULE:f(0),
27+
{hello, hello} = ?MODULE:f(2),
28+
% There are 35 terms available on heap.
29+
R = ?MODULE:g(34, ?MODULE:f(2)),
30+
{R} = ?MODULE:g(1, R),
31+
0.
2732

2833
f(N) ->
2934
erlang:make_tuple(N, hello).
3035

31-
g({}) ->
32-
1;
33-
g({hello}) ->
34-
2;
35-
g({hello, hello}) ->
36-
3;
37-
g(_) ->
38-
100.
36+
g(N, Elem) ->
37+
erlang:make_tuple(N, Elem).

tests/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ struct Test tests[] = {
178178
TEST_CASE_EXPECTED(test_insert_element, 121),
179179
TEST_CASE_EXPECTED(test_delete_element, 421),
180180
TEST_CASE_EXPECTED(test_tuple_to_list, 300),
181-
TEST_CASE_EXPECTED(test_make_tuple, 4),
181+
TEST_CASE(test_make_tuple),
182182
TEST_CASE_EXPECTED(test_make_list, 5),
183183
TEST_CASE_EXPECTED(test_list_gc, 2),
184184
TEST_CASE_EXPECTED(test_list_processes, 3),

0 commit comments

Comments
 (0)