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

Bugs in scratch allocator #1

Open
jkunstwald opened this issue Oct 18, 2020 · 1 comment
Open

Bugs in scratch allocator #1

jkunstwald opened this issue Oct 18, 2020 · 1 comment

Comments

@jkunstwald
Copy link

I've found two bugs in the scratch allocator (memory.cpp):

  1. If the ring buffer head (_allocate) ends up exactly at the end of the buffer after a call to allocate, it will never be reached by the while-loop in deallocate - the last condition in that loop checks equality with _end and wraps around to _begin - resulting in an infinite loop.
    This is fixed by if (_allocate == _end) { _allocate = _begin; } before returning.

  2. Allocating more memory than available incorrectly returns an OOB pointer if the ring buffer is otherwise empty, because the check is based on in_use(p), which immediately returns false if _free == _allocate.
    This can be fixed by extending the condition to if (p > _end || in_use(p)).

@jkunstwald
Copy link
Author

As a follow up, there is at least one additional bug but i haven't tracked it down, the symptoms are misdiagnosed double-frees and memory corruption. I wouldn't recommend using this allocator

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

1 participant