-
Notifications
You must be signed in to change notification settings - Fork 103
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
Verify that the requested allocation size does not overflow. #109
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -21,6 +21,7 @@ extern "C" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <assert.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <stddef.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <stdint.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <string.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include "./common.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -166,6 +167,12 @@ rcutils_string_map_reserve(rcutils_string_map_t * string_map, size_t capacity) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
// note that realloc when the pointer is NULL is the same as malloc | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// note also that realloc will shrink the space if needed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// ensure that reallocate won't overflow capacity | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (capacity > (SIZE_MAX / sizeof(char *))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain in what cases this might occur? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not trace down all the callers of rcutils_string_map_reserve so it may not occur in practice today. However, it may occur if the requested capacity is greater than SIZE_MAX / 4 or SIZE_MAX / 8, depending on the pointer size. I'll also note that glibc provides an explicit reallocarray() function that is the same as realloc() but includes the overflow check as done here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can consider adding a wrapper function in
Otherwise, this check here seems fine. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
RCUTILS_SET_ERROR_MSG("requested capacity for string_map too large", allocator) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return RCUTILS_RET_BAD_ALLOC; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// resize the keys, assigning the result only if it succeeds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
char ** new_keys = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
allocator.reallocate(string_map->impl->keys, capacity * sizeof(char *), allocator.state); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the overflow case? If so a comment would be good to indicate this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is the overflow case. I can add a comment.