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

test cache page allocation failure #464

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ add_page( struct cache *c ) {
if( !new_pages ) {
return -1;
}
c->pages = new_pages;

new_page = alloc_mem( c->page_size );
if( !new_page ) {
Expand All @@ -80,7 +81,6 @@ add_page( struct cache *c ) {
new_page_index = c->page_count;
c->page_count++;

c->pages = new_pages;
c->pages[new_page_index] = new_page;
init_page( c, new_page_index );

Expand Down
42 changes: 42 additions & 0 deletions test/function/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,48 @@ namespace {
stumpless_free_all( );
}

TEST(NewEntryTest, MallocFailureAfterCacheFill) {
struct stumpless_entry *entries[2000];
size_t i,j;
const char *app_name = "test-app-name";
const char *msgid = "test-msgid";
void *(*set_malloc_result)(size_t);

// create an entry to initialize the cache
entries[0] = stumpless_new_entry( STUMPLESS_FACILITY_USER,
STUMPLESS_SEVERITY_INFO,
app_name,
msgid,
NULL );
EXPECT_NOT_NULL(entries[0]);

set_malloc_result = stumpless_set_malloc(MALLOC_FAIL);
ASSERT_NOT_NULL(set_malloc_result);

for (i = 1; i < 2000; i++) {
entries[i] = stumpless_new_entry( STUMPLESS_FACILITY_USER,
STUMPLESS_SEVERITY_INFO,
app_name,
msgid,
NULL );

if (!entries[i]) {
EXPECT_ERROR_ID_EQ(STUMPLESS_MEMORY_ALLOCATION_FAILURE);
break;
}
}

EXPECT_NE(i, 2000);

set_malloc_result = stumpless_set_malloc(malloc);
EXPECT_TRUE(set_malloc_result == malloc);

for (j = 0; j < i; j++) {
stumpless_destroy_entry_and_contents(entries[j]);
}
stumpless_free_all();
}

TEST( NewEntryTest, MallocFailureOnMsgid ) {
void *(*set_malloc_result)(size_t);
const char *app_name = "test-app-name";
Expand Down
Loading