Skip to content

Commit 3bfabb9

Browse files
committed
NIFs: handle out of memory in binary_to_atom
Check malloc return value when allocating a new atom and raise error in case of failed alloc. Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent 3fdfb77 commit 3bfabb9

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ integers
4343
- Fix error handling when calling `min` and `max` with code compiled before OTP-26: there was a
4444
bug when handling errors from BIFs used as NIFs (when called with `CALL_EXT` and similar opcodes)`
4545
- Fix matching of binaries on unaligned boundaries for code compiled with older versions of OTP
46+
- Add missing out of memory handling in binary_to_atom
4647

4748
## [0.6.5] - 2024-10-15
4849

src/libAtomVM/nifs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,12 +2015,18 @@ static term binary_to_atom(Context *ctx, int argc, term argv[], int create_new)
20152015
}
20162016

20172017
atom = malloc(atom_string_len + 1);
2018+
if (IS_NULL_PTR(atom)) {
2019+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
2020+
}
20182021
((uint8_t *) atom)[0] = atom_string_len;
20192022
memcpy(((char *) atom) + 1, atom_string, atom_string_len);
20202023
} else {
20212024
// * 2 is the worst case size
20222025
size_t buf_len = atom_string_len * 2;
20232026
atom = malloc(buf_len + 1);
2027+
if (IS_NULL_PTR(atom)) {
2028+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
2029+
}
20242030
uint8_t *atom_data = ((uint8_t *) atom) + 1;
20252031
size_t out_pos = 0;
20262032
for (size_t i = 0; i < atom_string_len; i++) {

0 commit comments

Comments
 (0)