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

Fix unicode bogus oom #1280

Merged
merged 2 commits into from
Sep 24, 2024
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 @@ -30,6 +30,7 @@ instead
- ESP32: `Fixed gpio:set_int` to accept any pin, not only pin 2
- Fix memory corruption in `unicode:characters_to_binary`
- Fix handling of large literal indexes
- `unicode:characters_to_list`: fixed bogus out_of_memory error on some platforms such as ESP32

## [0.6.4] - 2024-08-18

Expand Down
5 changes: 4 additions & 1 deletion src/libAtomVM/nifs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4552,7 +4552,10 @@ static term nif_unicode_characters_to_list(Context *ctx, int argc, term argv[])
}
size_t len = size / sizeof(uint32_t);
uint32_t *chars = malloc(size);
if (IS_NULL_PTR(chars)) {
// fun fact: malloc(size) when size is 0, on some platforms may return NULL, causing a failure here
// so in order to avoid out_of_memory (while having plenty of memory) let's treat size==0 as a
// special case
if (UNLIKELY((chars == NULL) && (size != 0))) {
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
}
size_t needed_terms = CONS_SIZE * len;
Expand Down
28 changes: 28 additions & 0 deletions tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ struct Test
#define SKIP_STACKTRACES false
#endif

// Enabling this will override malloc and calloc weak symbols,
// so we can force an alternative version of malloc that returns
// NULL when size is 0.
// This is useful to find debugging or finding some kind of issues.
#ifdef FORCE_MALLOC_ZERO_RETURNS_NULL
void *malloc(size_t size)
{
if (size == 0) {
return NULL;
} else {
void *memptr = NULL;
if (posix_memalign(&memptr, sizeof(void *), size) != 0) {
return NULL;
}
return memptr;
}
}

void *calloc(size_t nmemb, size_t size)
{
void *ptr = malloc(nmemb * size);
if (ptr != NULL) {
memset(ptr, 0, nmemb * size);
}
return ptr;
}
#endif

struct Test tests[] = {
TEST_CASE_EXPECTED(add, 17),
TEST_CASE_EXPECTED(fact, 120),
Expand Down
Loading