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

_sbrk has no bounds checking #138

Open
WestfW opened this issue Aug 19, 2022 · 2 comments
Open

_sbrk has no bounds checking #138

WestfW opened this issue Aug 19, 2022 · 2 comments

Comments

@WestfW
Copy link
Contributor

WestfW commented Aug 19, 2022

_sbrk() in syscalls_sam3.c, does not contain any checks to see if the code is trying to acquired non-existent memory. This can lead malloc() and thus new to return pointers to either non-existent memory, or memory that belongs to the stack.

See https://forum.arduino.cc/t/ungraceful-handling-of-heap-depletion/1023509

@matthijskooijman
Copy link
Collaborator

I recently ran into this for samd too, which I think is similar to sam. I have a local fix I should create a pullrequest for, but got distracted with other things before I could finish it. I'll try to see if I can push it out maybe next week, though I probably won't have time to port it to sam as well.

@matthijskooijman
Copy link
Collaborator

I just published my fix for samd at arduino/ArduinoCore-samd#681, see that PR for a more detailed analysis (also check the commit messages) and a test sketch.

Looking at the sam core, it seems that the problem is essentially the same (sbrk() never failing), but where samd uses gcc's libnosys to provide it, the sam core (like @WestfW already mentioned) just contains a broken _sbrk() itself:

extern caddr_t _sbrk ( int incr )

The fix is the same, though, just add a proper _sbrk() implementation. At first glance the samd version from my PR would be suitable as-is, except samd uses end and sam uses _end to signal the end of global variables.

I won't have time to prepare a PR for this, but would be happy to review and assist if anyone else does so.

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