Skip to content

Conversation

@jmjaffe37
Copy link

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.

@jmjaffe37 jmjaffe37 changed the title Now ensuring that enum index is positive: [SM02915] EnumIndex Now ensuring that enum index is positive Sep 3, 2025

int MemoryManager::add_pool(MemoryPool* pool) {
int index = _num_pools;
assert(index < MemoryManager::max_num_pools, "_num_pools exceeds the max");
Copy link
Member

Choose a reason for hiding this comment

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

We need an assert on the lower bound as well

Copy link
Author

Choose a reason for hiding this comment

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

@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");

@jmjaffe37 jmjaffe37 marked this pull request as draft September 4, 2025 17:32
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.

2 participants