Skip to content

Commit 98fcec5

Browse files
committed
fix(apache2): fix memoery leak on log and body filter
1 parent ca9ea5a commit 98fcec5

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

src/mod_redirectionio.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ static int redirectionio_match_handler(request_rec *r) {
127127
return DECLINED;
128128
}
129129

130+
apr_pool_pre_cleanup_register(r->pool, ctx, redirectionio_context_cleanup);
131+
130132
ctx->request = NULL;
131133
ctx->action = NULL;
132134
ctx->response_headers = NULL;
@@ -879,6 +881,8 @@ static void redirectionio_apache_log_callback(const char* log_str, const void* d
879881
if (level <= 1) {
880882
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, (request_rec *)data, "mod_redirectionio api error: %s", log_str);
881883
}
884+
885+
free((char *)log_str);
882886
}
883887

884888
static apr_status_t redirectionio_atoi(const char *line, apr_size_t len) {

src/redirectionio_protocol.c

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ static apr_status_t redirectionio_read_string(redirectionio_connection *conn, ch
2121

2222
static apr_status_t redirectionio_send_protocol_header(redirectionio_connection *conn, const char *project_key, apr_uint16_t command, request_rec *r);
2323

24-
static apr_status_t redirectionio_action_cleanup(void *action);
25-
26-
static apr_status_t redirectionio_request_cleanup(void *request);
27-
28-
static apr_status_t redirectionio_response_headers_cleanup(void *response_headers);
29-
3024
apr_status_t redirectionio_protocol_match(redirectionio_connection *conn, redirectionio_context *ctx, request_rec *r, const char *project_key) {
3125
apr_uint32_t alen;
3226
apr_status_t rv;
@@ -92,8 +86,6 @@ apr_status_t redirectionio_protocol_match(redirectionio_connection *conn, redire
9286

9387
redirectionio_request_set_remote_addr(ctx->request, r->connection->client_ip, config->trusted_proxies);
9488

95-
apr_pool_pre_cleanup_register(r->pool, ctx->request, redirectionio_request_cleanup);
96-
9789
// Serialize request
9890
request_serialized = redirectionio_request_json_serialize(ctx->request);
9991

@@ -155,10 +147,6 @@ apr_status_t redirectionio_protocol_match(redirectionio_connection *conn, redire
155147

156148
// Unserialize action
157149
ctx->action = (struct REDIRECTIONIO_Action *)redirectionio_action_json_deserialize(action_serialized);
158-
159-
if (ctx->action != NULL) {
160-
apr_pool_pre_cleanup_register(r->pool, ctx->action, redirectionio_action_cleanup);
161-
}
162150
}
163151

164152
return APR_SUCCESS;
@@ -259,7 +247,6 @@ apr_status_t redirectionio_protocol_send_filter_headers(redirectionio_context *c
259247
return APR_SUCCESS;
260248
}
261249

262-
apr_pool_pre_cleanup_register(r->pool, ctx->response_headers, redirectionio_response_headers_cleanup);
263250
apr_table_clear(r->headers_out);
264251

265252
while (first_header != NULL) {
@@ -448,31 +435,38 @@ static apr_status_t redirectionio_send_protocol_header(redirectionio_connection
448435
return APR_SUCCESS;
449436
}
450437

451-
static apr_status_t redirectionio_action_cleanup(void *action) {
452-
redirectionio_action_drop(action);
438+
apr_status_t redirectionio_context_cleanup(void *context) {
439+
struct REDIRECTIONIO_HeaderMap *first_header, *tmp_header;
440+
redirectionio_context *ctx = (redirectionio_context *)context;
453441

454-
return APR_SUCCESS;
455-
}
442+
if (ctx->request != NULL) {
443+
redirectionio_request_drop(ctx->request);
444+
}
456445

457-
static apr_status_t redirectionio_request_cleanup(void *request) {
458-
redirectionio_request_drop(request);
446+
if (ctx->action != NULL) {
447+
redirectionio_action_drop(ctx->action);
448+
ctx->action = NULL;
449+
}
459450

460-
return APR_SUCCESS;
461-
}
451+
if (ctx->response_headers != NULL) {
452+
first_header = (struct REDIRECTIONIO_HeaderMap *)ctx->response_headers;
462453

463-
static apr_status_t redirectionio_response_headers_cleanup(void *response_headers) {
464-
struct REDIRECTIONIO_HeaderMap *first_header, *tmp_header;
454+
while (first_header != NULL) {
455+
tmp_header = first_header->next;
465456

466-
first_header = (struct REDIRECTIONIO_HeaderMap *)response_headers;
457+
free((void *)first_header->name);
458+
free((void *)first_header->value);
459+
free((void *)first_header);
467460

468-
while (first_header != NULL) {
469-
tmp_header = first_header->next;
461+
first_header = tmp_header;
462+
}
470463

471-
free((void *)first_header->name);
472-
free((void *)first_header->value);
473-
free((void *)first_header);
464+
ctx->response_headers = NULL;
465+
}
474466

475-
first_header = tmp_header;
467+
if (ctx->body_filter != NULL) {
468+
redirectionio_action_body_filter_drop(ctx->body_filter);
469+
ctx->body_filter = NULL;
476470
}
477471

478472
return APR_SUCCESS;

src/redirectionio_protocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ const char *ap_run_http_scheme(const request_rec *r);
1515
apr_status_t redirectionio_protocol_match(redirectionio_connection *conn, redirectionio_context *ctx, request_rec *r, const char *project_key);
1616
apr_status_t redirectionio_protocol_log(redirectionio_connection *conn, redirectionio_context *ctx, request_rec *r, const char *project_key);
1717
apr_status_t redirectionio_protocol_send_filter_headers(redirectionio_context *ctx, request_rec *r);
18+
apr_status_t redirectionio_context_cleanup(void *redirectionio_context);
1819

1920
#endif

0 commit comments

Comments
 (0)