Skip to content

Commit e67a4ad

Browse files
authored
validate the allocator before use. (#455)
* validate the allocator before use. Signed-off-by: Tomoya Fujita <[email protected]> * address review comments. - validate allocator only if the function specifically uses. - argument null check comes before validation of value. Signed-off-by: Tomoya Fujita <[email protected]> --------- Signed-off-by: Tomoya Fujita <[email protected]>
1 parent 5608495 commit e67a4ad

File tree

5 files changed

+13
-0
lines changed

5 files changed

+13
-0
lines changed

src/filesystem.c

+3
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ rcutils_calculate_directory_size_with_recursion(
365365
return RCUTILS_RET_ERROR;
366366
}
367367

368+
RCUTILS_CHECK_ALLOCATOR(&allocator, return RCUTILS_RET_INVALID_ARGUMENT);
368369
dir_list = allocator.zero_allocate(1, sizeof(dir_list_t), allocator.state);
369370
if (NULL == dir_list) {
370371
RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to allocate memory !\n");
@@ -508,6 +509,8 @@ rcutils_dir_iter_end(rcutils_dir_iter_t * iter)
508509
}
509510

510511
rcutils_allocator_t allocator = iter->allocator;
512+
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
513+
&allocator, "allocator is invalid", return );
511514
rcutils_dir_iter_state_t * state = (rcutils_dir_iter_state_t *)iter->state;
512515
if (NULL != state) {
513516
#ifdef _WIN32

src/hash_map.c

+2
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ rcutils_hash_map_set(rcutils_hash_map_t * hash_map, const void * key, const void
402402
} else {
403403
// We need to create a new entry in the map
404404
rcutils_allocator_t * allocator = &hash_map->impl->allocator;
405+
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
406+
allocator, "allocator is invalid", return RCUTILS_RET_INVALID_ARGUMENT);
405407

406408
// Start by trying to allocate the memory we need for the new entry
407409
entry = allocator->allocate(sizeof(rcutils_hash_map_entry_t), allocator->state);

src/repl_str.c

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ rcutils_repl_str(
4949
const char * to,
5050
const rcutils_allocator_t * allocator)
5151
{
52+
RCUTILS_CHECK_ALLOCATOR(allocator, return NULL);
5253
/* Adjust each of the below values to suit your needs. */
5354

5455
/* Increment positions cache size initially by this number. */

src/strdup.c

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ rcutils_strndup(const char * str, size_t max_length, rcutils_allocator_t allocat
4343
if (NULL == str) {
4444
return NULL;
4545
}
46+
RCUTILS_CHECK_ALLOCATOR(&allocator, return NULL);
4647
char * p = memchr(str, '\0', max_length);
4748
size_t string_length = p == NULL ? max_length : (size_t)(p - str);
4849
char * new_string = allocator.allocate(string_length + 1, allocator.state);

src/string_map.c

+6
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ rcutils_string_map_fini(rcutils_string_map_t * string_map)
101101
return ret;
102102
}
103103
rcutils_allocator_t allocator = string_map->impl->allocator;
104+
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
105+
&allocator, "allocator is invalid", return RCUTILS_RET_INVALID_ARGUMENT);
104106

105107
allocator.deallocate(string_map->impl, allocator.state);
106108
string_map->impl = NULL;
@@ -152,6 +154,8 @@ rcutils_string_map_reserve(rcutils_string_map_t * string_map, size_t capacity)
152154
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
153155
string_map->impl, "invalid string map", return RCUTILS_RET_STRING_MAP_INVALID);
154156
rcutils_allocator_t allocator = string_map->impl->allocator;
157+
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
158+
&allocator, "allocator is invalid", return RCUTILS_RET_INVALID_ARGUMENT);
155159
// short circuit, if requested capacity is less than the size of the map
156160
if (capacity < string_map->impl->size) {
157161
// set the capacity to the current size instead
@@ -276,6 +280,8 @@ rcutils_string_map_set_no_resize(
276280
RCUTILS_CHECK_ARGUMENT_FOR_NULL(key, RCUTILS_RET_INVALID_ARGUMENT);
277281
RCUTILS_CHECK_ARGUMENT_FOR_NULL(value, RCUTILS_RET_INVALID_ARGUMENT);
278282
rcutils_allocator_t allocator = string_map->impl->allocator;
283+
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
284+
&allocator, "allocator is invalid", return RCUTILS_RET_INVALID_ARGUMENT);
279285
size_t key_index;
280286
bool should_free_key_on_error = false;
281287
bool key_exists = __get_index_of_key_if_exists(string_map->impl, key, strlen(key), &key_index);

0 commit comments

Comments
 (0)