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

avoid invalid iterator dereference in get_allocator() #34

Merged
merged 1 commit into from
Jan 10, 2024

Conversation

spoonincode
Copy link
Member

get_allocator() needs to return the bip allocator for a given pointer based on looking up that pointer in a global map of address ranges, or return nullopt_t if the pointer is not within any address range (which means it wasn't allocated in a bip allocator, such as being allocated on stack).

Consider there is a single range registered (_segment_manager_map.size() == 1) and this range is from 0x2000 to 0x4000. get_allocator(0x5000) will cause upper_bound() to return end(). Then reversing the iterator by one via *(--it) will return the 0x2000-0x4000 entry, the if (object < seg_end) check fails, and nullopt_t is returned as expected.

But get_allocator(0x1000) will return begin(). This causes the *(--it) to iterate off the valid range and dereference an invalid iterator.

Add a check for begin() and immediately return nullopt_t.

Copy link
Member

@heifner heifner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a test case.

@spoonincode
Copy link
Member Author

Consider adding a test case.

Fortunately, the existing shared_vector_apis_segment_alloc test was running afoul here

@spoonincode spoonincode merged commit 9d1c472 into main Jan 10, 2024
2 checks passed
@spoonincode spoonincode deleted the get_allocator_iteratorcheck branch January 10, 2024 01:28
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

Successfully merging this pull request may close these issues.

3 participants