Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing out-of-memory handling in binary_to_atom #1535

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading