Skip to content

Commit 9029347

Browse files
committed
Fix memory leak in capture tests by using test_http_write callback
The capture tests (large_body_capture, grwprintf_truncation_bug) were calling hs_request_begin_write which would call hs_request_terminate_connection on write error, freeing the request. But the tests then tried to use the captured buffer which was now owned by the request. Fix: Have test_http_write callback also capture bytes when in CAPTURE mode by calling hs_test_write directly. This allows proper cleanup via destroy_test_request without double-free. Also adds hs_test_write declaration to test_write_socket.h for proper extern access.
1 parent d002a84 commit 9029347

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

test/unit/test_respond.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ static int http_write_call_count = 0;
1212

1313
static void test_http_write(struct http_request_s* request) {
1414
http_write_call_count++;
15+
if (hs_test_write_mode == HS_TEST_WRITE_CAPTURE) {
16+
hs_test_enable_write_stub(1);
17+
hs_test_write(request->socket, request->buffer.buf, request->buffer.length);
18+
}
1519
}
1620

1721
static struct http_request_s* setup_test_request(void) {
@@ -111,7 +115,7 @@ MunitResult test_respond_large_body_capture(const MunitParameter params[], void*
111115
memset(large_body, 'X', 2048);
112116
hs_response_set_body(response, large_body, 2048);
113117

114-
hs_request_respond(request, response, hs_request_begin_write);
118+
hs_request_respond(request, response, test_http_write);
115119

116120
char* headers_end = strstr(captured_write_buf, "\r\n\r\n");
117121
munit_assert_ptr_not_null(headers_end);
@@ -124,6 +128,7 @@ MunitResult test_respond_large_body_capture(const MunitParameter params[], void*
124128

125129
hs_test_enable_write_stub(0);
126130
hs_test_reset_capture();
131+
destroy_test_request(request);
127132

128133
return MUNIT_OK;
129134
}
@@ -156,7 +161,7 @@ MunitResult test_respond_grwprintf_truncation_bug(const MunitParameter params[],
156161
memset(body, 'Y', 64);
157162
hs_response_set_body(response, body, 64);
158163

159-
hs_request_respond(request, response, hs_request_begin_write);
164+
hs_request_respond(request, response, test_http_write);
160165

161166
munit_assert_ptr_not_null(captured_write_buf);
162167
munit_assert_size(captured_write_size, >, 0);
@@ -179,6 +184,7 @@ MunitResult test_respond_grwprintf_truncation_bug(const MunitParameter params[],
179184

180185
hs_test_enable_write_stub(0);
181186
hs_test_reset_capture();
187+
destroy_test_request(request);
182188

183189
return MUNIT_OK;
184190
}

test/unit/test_write_socket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extern enum hs_test_write_mode_e hs_test_write_mode;
1111

1212
void hs_test_enable_write_stub(int enabled);
1313
void hs_test_reset_capture(void);
14+
ssize_t hs_test_write(int fd, char const *data, size_t size);
1415
extern char* captured_write_buf;
1516
extern size_t captured_write_size;
1617

0 commit comments

Comments
 (0)