Skip to content

Commit

Permalink
NIFs: handle out of memory in binary_to_atom
Browse files Browse the repository at this point in the history
Check malloc return value when allocating a new atom and raise error in
case of failed alloc.

Signed-off-by: Davide Bettio <[email protected]>
  • Loading branch information
bettio committed Feb 14, 2025
1 parent 3fdfb77 commit 3bfabb9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ integers
- Fix error handling when calling `min` and `max` with code compiled before OTP-26: there was a
bug when handling errors from BIFs used as NIFs (when called with `CALL_EXT` and similar opcodes)`
- Fix matching of binaries on unaligned boundaries for code compiled with older versions of OTP
- Add missing out of memory handling in binary_to_atom

## [0.6.5] - 2024-10-15

Expand Down
6 changes: 6 additions & 0 deletions src/libAtomVM/nifs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2015,12 +2015,18 @@ static term binary_to_atom(Context *ctx, int argc, term argv[], int create_new)
}

atom = malloc(atom_string_len + 1);
if (IS_NULL_PTR(atom)) {
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
}
((uint8_t *) atom)[0] = atom_string_len;
memcpy(((char *) atom) + 1, atom_string, atom_string_len);
} else {
// * 2 is the worst case size
size_t buf_len = atom_string_len * 2;
atom = malloc(buf_len + 1);
if (IS_NULL_PTR(atom)) {
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
}
uint8_t *atom_data = ((uint8_t *) atom) + 1;
size_t out_pos = 0;
for (size_t i = 0; i < atom_string_len; i++) {
Expand Down

0 comments on commit 3bfabb9

Please sign in to comment.