From 6325f2860665826c84c19dbcbf740b522ef91046 Mon Sep 17 00:00:00 2001 From: Mohd Bilal Date: Mon, 7 Oct 2024 11:12:44 +0530 Subject: [PATCH 1/3] test cache page allocation failure --- test/function/entry.cpp | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/function/entry.cpp b/test/function/entry.cpp index 6c137092..04213677 100644 --- a/test/function/entry.cpp +++ b/test/function/entry.cpp @@ -1866,6 +1866,49 @@ namespace { stumpless_free_all( ); } + TEST(NewEntryTest, MallocFailureAfterCacheFill) { + struct stumpless_entry *entries[2000]; + struct stumpless_entry *entry; + 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"; From 99508249190e7e163c97b5ebb8ba73ee416718dc Mon Sep 17 00:00:00 2001 From: Mohd Bilal Date: Mon, 7 Oct 2024 11:32:27 +0530 Subject: [PATCH 2/3] remove unused entry pointer --- test/function/entry.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/test/function/entry.cpp b/test/function/entry.cpp index 04213677..f6755b66 100644 --- a/test/function/entry.cpp +++ b/test/function/entry.cpp @@ -1868,7 +1868,6 @@ namespace { TEST(NewEntryTest, MallocFailureAfterCacheFill) { struct stumpless_entry *entries[2000]; - struct stumpless_entry *entry; size_t i,j; const char *app_name = "test-app-name"; const char *msgid = "test-msgid"; From 26a252e5b2b58a2468be154dc02003733b557789 Mon Sep 17 00:00:00 2001 From: Mohd Bilal Date: Mon, 7 Oct 2024 17:21:56 +0530 Subject: [PATCH 3/3] fix memory leak this updates cache structure before the alloc_mem call so that the structure is intact even in case of an allocation failure --- src/cache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cache.c b/src/cache.c index a7e5d0c7..d39770b6 100644 --- a/src/cache.c +++ b/src/cache.c @@ -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 ) { @@ -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 );