Skip to contentSkip to conversation

Fix memory leak in buffer unit tests

PATCH In review Steffan Karger 3 participants 3 messages Dec 8, 2017 – Jan 9, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix memory leak in buffer unit tests

buffer_list_push_data does not take ownership of the memory, so just feed it stack data to plug the leak.

---
 tests/unit_tests/openvpn/test_buffer.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
tests/unit_tests/openvpn/test_buffer.c +3 -4
index ba5aa67..c2b7f30 100644
@@ -77,10 +77,9 @@ static int test_buffer_list_setup(void **state)
     buffer_list_push(ctx->zero_length_strings, "");
 
     ctx->empty_buffers = buffer_list_new(2);
-    uint8_t *data1 = malloc(1);
-    uint8_t *data2 = malloc(1);
-    buffer_list_push_data(ctx->empty_buffers, data1, 0);
-    buffer_list_push_data(ctx->empty_buffers, data2, 0);
+    uint8_t data = 0;
+    buffer_list_push_data(ctx->empty_buffers, &data, 0);
+    buffer_list_push_data(ctx->empty_buffers, &data, 0);
 
     *state = ctx;
     return 0;
Signature · 1 line
2.7.4
Acked-by on v1 1/1
#1 lore raw

Hi,

Quoted 4 lines · Steffan Karger

buffer_list_push_data does not take ownership of the memory, so just feed it stack data to plug the leak.

as reported by Steffan, buffer_list_push_data() takes the content of the buffer passed as argument and copies it in a new buffer. Thus the ownership of that argument still belongs to the caller which is responsible for releasing it.

Using stack data just fixes the issue.

Aside from this patch, I just noted that buffer_list_push_data() suffers from the same problem as buffer_list_aggregate_separator(). The caller could pass size=0 as argument, thus leading to a 0-byte malloc. This should probably be fixed with an additional patch.

Cheers,

Quoted 23 lines
---
 tests/unit_tests/openvpn/test_buffer.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
tests/unit_tests/openvpn/test_buffer.c +3 -4
index ba5aa67..c2b7f30 100644
@@ -77,10 +77,9 @@ static int test_buffer_list_setup(void **state)
     buffer_list_push(ctx->zero_length_strings, "");
 
     ctx->empty_buffers = buffer_list_new(2);
-    uint8_t *data1 = malloc(1);
-    uint8_t *data2 = malloc(1);
-    buffer_list_push_data(ctx->empty_buffers, data1, 0);
-    buffer_list_push_data(ctx->empty_buffers, data2, 0);
+    uint8_t data = 0;
+    buffer_list_push_data(ctx->empty_buffers, &data, 0);
+    buffer_list_push_data(ctx->empty_buffers, &data, 0);
 
     *state = ctx;
     return 0;
Signature · 1 line
Antonio Quartulli
#2 lore raw
In reply to Steffan Karger

Your patch has been applied to the master and release/2.4 branch.

commit 2c7c760dfbddbc9cf348bce06fa922c1217a2039 (master) commit 888204a1e9a4278a62e9a1cb0ea1c0cdff95473e (release/2.4)

Author: Steffan Karger
Date:   Fri Dec 8 10:12:18 2017 +0100
     Fix memory leak in buffer unit tests

     Signed-off-by: Steffan Karger <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg16055.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering