This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Why was a malloc with heap_size = 0 successful?

We started with the default project that have no heap. Added mallocs to the system and they seemed to succeed, without realizing heap was set to 0. They did not return NULL.

I would have expected the mallocs to fail since there is no heap at all.

Can anyone explain why the malloc succeeded?

Parents
  • What compiler/stdlib are you using? malloc is normally provided by an in-built library, unless you're brave and declare your own.

    I know that the malloc implementation in libnosys (-lnosys in your linker file, if using GCC) does not have any overflow check in function _sbrk(), so if you're using GCC + nosys; this will happen and it will most likely eat out of the bottom of the .stack. Here's the _sbrk() from libnosys, which basically has no checks: github.com/.../sbrk.c

    Cheers, Håkon

Reply
  • What compiler/stdlib are you using? malloc is normally provided by an in-built library, unless you're brave and declare your own.

    I know that the malloc implementation in libnosys (-lnosys in your linker file, if using GCC) does not have any overflow check in function _sbrk(), so if you're using GCC + nosys; this will happen and it will most likely eat out of the bottom of the .stack. Here's the _sbrk() from libnosys, which basically has no checks: github.com/.../sbrk.c

    Cheers, Håkon

Children
Related