Now ensuring that enum index is positive#34
Draft
jmjaffe37 wants to merge 1 commit intomicrosoft:mainfrom
Draft
Conversation
swesonga
reviewed
Sep 4, 2025
| @@ -51,7 +51,7 @@ MemoryManager::MemoryManager(const char* name) | |||
| int MemoryManager::add_pool(MemoryPool* pool) { | |||
| int index = _num_pools; | |||
| assert(index < MemoryManager::max_num_pools, "_num_pools exceeds the max"); | |||
Member
There was a problem hiding this comment.
We need an assert on the lower bound as well
Author
There was a problem hiding this comment.
@swesonga what should the lower bound be? Should it be 1? Here is an example line that I could add:
assert(index >= 1, "_num_pools must be at least 1");
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When accessing array _pools with index index, the upper bound of an enum is used to check the upper bound of the array, but the lower bound is not checked. This simple PR fixes this issue
Code using enumerated types as indexes into arrays will often check for an upper bound to ensure the index is not out of range. By default an enum variable is signed, and therefore it is important to ensure that it cannot take on a negative value. When the enum is subsequently used to index an array, or worse still an array of function pointers, then a negative enum value would lead to potentially arbitrary memory being read, used and/or executed.