Skip to content

Commit

Permalink
add realloc_array function
Browse files Browse the repository at this point in the history
Adds a new memory reallocation function for arrays which
includes an overflow check on the size multiplication.
  • Loading branch information
m3g4d1v3r authored Nov 2, 2024
1 parent 5f68b8f commit b638f77
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
5 changes: 5 additions & 0 deletions include/private/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ size_t get_paged_size( size_t size );
* allocation failure error.
*/
void *realloc_mem( const void *mem, size_t size );

// A wrapper for alloc_mem that checks for overflow before proper allocation
void *alloc_array( size_t item_count, size_t item_size );

// A wrapper for realloc_mem that checks for overflow before proper allocation
void *realloc_array( const void *mem, size_t item_count, size_t item_size );

#endif /* __STUMPLESS_PRIVATE_MEMORY_H */
2 changes: 1 addition & 1 deletion src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ add_page( struct cache *c ) {
char **new_pages;
char *new_page;

new_pages = realloc_mem( c->pages, sizeof( char * ) * ( c->page_count + 1 ) );
new_pages = realloc_array( c->pages, ( c->page_count + 1 ), sizeof( char * ) );
if( !new_pages ) {
return -1;
}
Expand Down
7 changes: 1 addition & 6 deletions src/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,13 @@ struct stumpless_element *
stumpless_add_param( struct stumpless_element *element,
struct stumpless_param *param ) {
struct stumpless_param **new_params;
size_t old_params_size;
size_t new_params_size;

VALIDATE_ARG_NOT_NULL( element );
VALIDATE_ARG_NOT_NULL( param );

lock_element( element );

old_params_size = sizeof( param ) * element->param_count;
new_params_size = old_params_size + sizeof( param );

new_params = realloc_mem( element->params, new_params_size );
new_params = realloc_array( element->params, element->param_count + 1, sizeof( struct stumpless_param * ) );
if( !new_params ) {
unlock_element( element );
return NULL;
Expand Down
11 changes: 10 additions & 1 deletion src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ alloc_array( size_t item_count, size_t item_size ) {
return alloc_mem(item_count * item_size);
}

void *
realloc_array( const void *mem, size_t item_count, size_t item_size ) {
if (item_count && item_count >= (size_t)-1/item_size) {
raise_memory_allocation_failure();
return NULL;
}
return realloc_mem(mem, item_count * item_size);
}

malloc_func_t stumpless_get_malloc(void) {
return stumpless_malloc ? stumpless_malloc : NULL;
}
Expand All @@ -146,4 +155,4 @@ free_func_t stumpless_get_free(void) {

realloc_func_t stumpless_get_realloc(void) {
return stumpless_realloc ? stumpless_realloc : NULL;
}
}
2 changes: 1 addition & 1 deletion src/target/journald.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void
init_fields( size_t field_count ) {
struct iovec *new_fields;

new_fields = realloc_mem( fields, sizeof( *fields ) * field_count );
new_fields = realloc_array( fields, field_count, sizeof( *fields ) );
if( !new_fields ) {
return;
}
Expand Down
17 changes: 17 additions & 0 deletions test/function/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,23 @@ namespace {
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}

TEST_F( ElementTest, HighParamCount ) {
size_t original_param_count;
const struct stumpless_element *result;

original_param_count = stumpless_get_param_count( basic_element );
basic_element->param_count = SIZE_MAX - 1;

result = stumpless_add_new_param( basic_element, "high-param-count-name", "high-param-count-value" );
EXPECT_NULL( result );

EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE );

basic_element->param_count = original_param_count;
EXPECT_EQ( stumpless_get_param_count( basic_element ),
original_param_count );
}

TEST_F( ElementTest, SetName ) {
const char *new_name = "awesome-new-name";
const struct stumpless_element *result;
Expand Down

0 comments on commit b638f77

Please sign in to comment.