Skip to content
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

Adding an alloc_array function #458

Merged
merged 5 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/private/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ void *alloc_mem( size_t size );
void free_mem( const void *mem );
size_t get_paged_size( size_t size );
void *realloc_mem( const void *mem, size_t size );
void *alloc_array( size_t item_count, size_t item_size );

#endif /* __STUMPLESS_PRIVATE_MEMORY_H */
4 changes: 2 additions & 2 deletions src/config/have_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ windows_copy_cstring_to_lpwstr( LPCSTR str, int *copy_length ) {
return NULL;
}

str_copy = alloc_mem( needed_wchar_length * sizeof( WCHAR ) );
str_copy = alloc_array( needed_wchar_length, sizeof( WCHAR ) );
if( !str_copy ) {
return NULL;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ windows_copy_wstring_to_cstring( const wchar_t *str, int *copy_size ){
return NULL;
}

str_copy = alloc_mem( needed_size * sizeof( char ) );
str_copy = alloc_array( needed_size, sizeof( char ) );
if( !str_copy ) {
return NULL;
}
Expand Down
6 changes: 3 additions & 3 deletions src/config/wel_supported.c
Original file line number Diff line number Diff line change
Expand Up @@ -1898,7 +1898,7 @@ copy_param_value_to_lpwstr( const struct stumpless_param *param ) {
}

needed_wchar_count = ( ( size_t ) needed_wchar_length ) + 1;
str_copy = alloc_mem( needed_wchar_count * sizeof( WCHAR ) );
str_copy = alloc_array( needed_wchar_count, sizeof( WCHAR ) );
if( !str_copy ) {
goto fail;
}
Expand Down Expand Up @@ -1952,12 +1952,12 @@ copy_wel_data( struct stumpless_entry *destination,


if( source_data->insertion_count > 0 ) {
dest_data->insertion_params = alloc_mem( sizeof( struct stumpless_param * ) * source_data->insertion_count );
dest_data->insertion_params = alloc_array( source_data->insertion_count, sizeof( struct stumpless_param ) );
if( !dest_data->insertion_params) {
goto fail;
}

dest_data->insertion_strings = alloc_mem( sizeof( LPCSTR ) * source_data->insertion_count );
dest_data->insertion_strings = alloc_array( source_data->insertion_count, sizeof( LPCSTR ) );
if( !dest_data->insertion_strings) {
goto fail_strings;
}
Expand Down
4 changes: 2 additions & 2 deletions src/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ stumpless_copy_element( const struct stumpless_element *element ) {
goto fail;
}

copy->params = alloc_mem( element->param_count * sizeof( param_copy ) );
copy->params = alloc_array( element->param_count, sizeof( param_copy ) );
if( !copy->params ) {
goto fail_param_copy;
}
Expand Down Expand Up @@ -212,7 +212,7 @@ stumpless_element_to_string( const struct stumpless_element *element ) {
// acc total format size
format_len = name_len;

params_format = alloc_mem(sizeof(char*) * param_count);
params_format = alloc_array( param_count, sizeof(char*) );
for( i = 0; i < param_count; i++ ) {
params_format[i] = stumpless_param_to_string(params[i]);
// does not count '\0' on purpose
Expand Down
2 changes: 1 addition & 1 deletion src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ stumpless_copy_entry( const struct stumpless_entry *entry ) {
goto cleanup_and_fail;
}

copy->elements = alloc_mem( entry->element_count * sizeof( element_copy ) );
copy->elements = alloc_array( entry->element_count, sizeof( element_copy ) );
if( !copy->elements ) {
goto fail_elements;
}
Expand Down
9 changes: 9 additions & 0 deletions src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,12 @@ realloc_mem( const void *mem, size_t size ) {

return new_mem;
}

void *
alloc_array( size_t item_count, size_t item_size ) {
if (item_count && item_count >= (size_t)-1/item_size) {
raise_memory_allocation_failure();
return NULL;
aadit-n3rdy marked this conversation as resolved.
Show resolved Hide resolved
}
return alloc_mem(item_count * item_size);
}
20 changes: 20 additions & 0 deletions test/function/entry.cpp
goatshriek marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ namespace {
const char *param_1_1_value = "basic-value";
struct stumpless_param *param_1_1 = NULL;
struct stumpless_entry *nil_entry = NULL;
struct stumpless_entry *large_entry = NULL;
size_t large_entry_ec;

virtual void
SetUp( void ) {
Expand All @@ -84,11 +86,21 @@ namespace {
stumpless_add_element( basic_entry, element_2 );

nil_entry = create_nil_entry();

large_entry = stumpless_new_entry_str( STUMPLESS_FACILITY_USER,
STUMPLESS_SEVERITY_INFO,
basic_app_name,
basic_msgid,
basic_message );
large_entry_ec = large_entry->element_count;
large_entry->element_count = SIZE_MAX;
}

virtual void
TearDown( void ){
stumpless_destroy_entry_and_contents( basic_entry );
large_entry->element_count = large_entry_ec;
stumpless_destroy_entry_and_contents(large_entry);
stumpless_destroy_entry_only( nil_entry );
stumpless_free_all( );
}
Expand Down Expand Up @@ -359,6 +371,14 @@ namespace {
EXPECT_TRUE( set_malloc_result == malloc );
}

TEST_F( EntryTest, CopyMallocFailureOnSizeOverflow ) {
const struct stumpless_entry *result;

result = stumpless_copy_entry( large_entry );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE );
aadit-n3rdy marked this conversation as resolved.
Show resolved Hide resolved
}

TEST_F( EntryTest, CopyReallocFailure ) {
const struct stumpless_entry *result;
void * (*set_realloc_result)(void *, size_t);
Expand Down
Loading