Skip to content

Commit c9eab07

Browse files
committed
Fix heap calculation in erlang:atom_to_binary/1,2
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent 90c8d7b commit c9eab07

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/libAtomVM/nifs.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,13 +2174,15 @@ static term nif_erlang_atom_to_binary(Context *ctx, int argc, term argv[])
21742174
RAISE_ERROR(BADARG_ATOM);
21752175
}
21762176

2177-
if (UNLIKELY(memory_ensure_free_opt(ctx, term_binary_heap_size(atom_len), MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
2178-
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
2179-
}
2180-
21812177
if (LIKELY(!encode_to_latin1)) {
2178+
if (UNLIKELY(memory_ensure_free_opt(ctx, TERM_BOXED_REFC_BINARY_SIZE, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
2179+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
2180+
}
21822181
return term_from_const_binary(atom_data, atom_len, &ctx->heap, glb);
21832182
} else {
2183+
if (UNLIKELY(memory_ensure_free_opt(ctx, term_binary_heap_size(atom_len), MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
2184+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
2185+
}
21842186
size_t encoded_len = unicode_buf_utf8_len(atom_data, atom_len);
21852187
term binary = term_create_uninitialized_binary(encoded_len, &ctx->heap, glb);
21862188
char *binary_data = (char *) term_binary_data(binary);

0 commit comments

Comments
 (0)