Skip to content

malloc(0) fails #120

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

Closed
rillian opened this issue Nov 17, 2011 · 3 comments
Closed

malloc(0) fails #120

rillian opened this issue Nov 17, 2011 · 3 comments

Comments

@rillian
Copy link

rillian commented Nov 17, 2011

Zero-length malloc calls assert. Since libc tends to return a valid pointer in this case, it's not uncommon. I found this particular one with one of the libogg framing.c unit tests from issue #115.

Calls to malloc(0) do succeed with --dlmalloc, but it would be nice if it were generally supported. Just allocate one space on the stack like you would for malloc(1)?

@rillian
Copy link
Author

rillian commented Nov 17, 2011

$ cat null-malloc.c
/** sample test to check malloc(0) behaviour */
#include <stdio.h>
#include <stdlib.h>

int test_malloc(int bytes)
{
char *p;

fprintf(stdout, "testing malloc(%d)...", bytes);
fflush(stdout);
p = malloc(bytes);
fprintf(stdout, "returned 0x%p\n", p);
fflush(stdout);
if (p) {
free(p);
return 0;
}

return 1;
}

int main(int argc, char *argv[])
{
int ret;

ret = test_malloc(1);
ret = test_malloc(0);

return ret;
}

@kripken
Copy link
Member

kripken commented Nov 17, 2011

Fixed in e2c5a4d, thanks for the bug report.

@kripken kripken closed this as completed Nov 17, 2011
@rillian
Copy link
Author

rillian commented Nov 17, 2011

Confirmed, thanks!

eagleoflqj pushed a commit to eagleoflqj/emscripten that referenced this issue Nov 16, 2024
lewing pushed a commit to lewing/emscripten that referenced this issue Mar 14, 2025
…128.2 (emscripten-core#120)

[dotnet/main] Update dependencies from dotnet/arcade
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants