Skip to content

Commit 67aab91

Browse files
committed
Merge pull request atomvm#515 from pguyot/w16/fix-binary_to_term-used-crash
Fix crash related to binary_to_term If `MIN_FREE_SPACE_SIZE` is 0, or if allocation strategy is fixed, this crashes. Fix the crash with a hack that consists in using ctx->x[0] as a GC root. 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 1930510 + 7850c78 commit 67aab91

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/libAtomVM/nifs.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2511,8 +2511,13 @@ static term nif_erlang_binary_to_term(Context *ctx, int argc, term argv[])
25112511
RAISE_ERROR(BADARG_ATOM)
25122512
}
25132513
if (return_used) {
2514+
// Trick: use x[0] as a gc root.
2515+
ctx->x[0] = dst;
2516+
if (UNLIKELY(memory_ensure_free(ctx, TUPLE_SIZE(2)) != MEMORY_GC_OK)) {
2517+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
2518+
}
25142519
term ret = term_alloc_tuple(2, ctx);
2515-
term_put_tuple_element(ret, 0, dst);
2520+
term_put_tuple_element(ret, 0, ctx->x[0]);
25162521
term_put_tuple_element(ret, 1, term_from_int(bytes_read));
25172522
return ret;
25182523
} else {

0 commit comments

Comments
 (0)