From f446fb34deb6c79278ae9a6e005ddc05692e6f12 Mon Sep 17 00:00:00 2001 From: uink45 <79078981+uink45@users.noreply.github.com> Date: Thu, 19 Feb 2026 00:16:28 +1000 Subject: [PATCH] Add memory leak fixes --- src/core/client_reqresp_blocks.c | 15 +++++++-------- src/internal/yaml_parser.c | 31 ++++++++++++++++++++++--------- src/networking/enr.c | 8 ++++---- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/core/client_reqresp_blocks.c b/src/core/client_reqresp_blocks.c index 07c7af6..bcda9ee 100644 --- a/src/core/client_reqresp_blocks.c +++ b/src/core/client_reqresp_blocks.c @@ -320,6 +320,13 @@ static void *block_request_worker(void *arg) .peer = ctx->peer_text[0] ? ctx->peer_text : NULL, }; + LanternBlocksByRootRequest request; + lantern_blocks_by_root_request_init(&request); + uint8_t *raw_request = NULL; + uint8_t *payload = NULL; + enum lantern_blocks_request_outcome outcome = LANTERN_BLOCKS_REQUEST_FAILED; + bool completed = false; + char root_hex[(LANTERN_ROOT_SIZE * 2u) + 3u]; if (!ctx->roots || ctx->root_count == 0) { @@ -331,14 +338,6 @@ static void *block_request_worker(void *arg) } format_root_hex(&ctx->roots[0], root_hex, sizeof(root_hex)); - LanternBlocksByRootRequest request; - lantern_blocks_by_root_request_init(&request); - - uint8_t *raw_request = NULL; - uint8_t *payload = NULL; - enum lantern_blocks_request_outcome outcome = LANTERN_BLOCKS_REQUEST_FAILED; - bool completed = false; - if (lantern_root_list_resize(&request.roots, ctx->root_count) != 0) { lantern_log_error( diff --git a/src/internal/yaml_parser.c b/src/internal/yaml_parser.c index 80ac912..3119264 100644 --- a/src/internal/yaml_parser.c +++ b/src/internal/yaml_parser.c @@ -96,20 +96,20 @@ static void add_pair(LanternYamlObject *obj, const char *key, const char *value) obj->num_pairs++; } -static void commit_current( +static int commit_current( LanternYamlObject *current, LanternYamlObject **objects, size_t *capacity, size_t *count) { if (!current || current->num_pairs == 0) { - return; + return 0; } if (*count == *capacity) { size_t new_capacity = (*capacity == 0) ? 4 : (*capacity * 2); LanternYamlObject *new_objects = realloc(*objects, new_capacity * sizeof(*new_objects)); if (!new_objects) { - return; + return -1; } *objects = new_objects; *capacity = new_capacity; @@ -121,6 +121,7 @@ static void commit_current( current->pairs = NULL; current->num_pairs = 0; current->capacity = 0; + return 0; } static void free_yaml_object(LanternYamlObject *object) { @@ -172,7 +173,10 @@ LanternYamlObject *lantern_yaml_read_array(const char *file_path, const char *ar if (content[0] == '-') { if (in_target_array) { - commit_current(¤t, &objects, &capacity, out_count); + if (commit_current(¤t, &objects, &capacity, out_count) != 0) { + parse_error = 1; + break; + } } content++; while (isspace((unsigned char)*content)) { @@ -185,10 +189,16 @@ LanternYamlObject *lantern_yaml_read_array(const char *file_path, const char *ar while (stack_size > 0 && indent_stack[stack_size - 1] >= indent) { pop_stack(keys_stack, &stack_size); if (in_target_array && stack_size < array_depth) { - commit_current(¤t, &objects, &capacity, out_count); + if (commit_current(¤t, &objects, &capacity, out_count) != 0) { + parse_error = 1; + break; + } in_target_array = 0; } } + if (parse_error) { + break; + } } char *sep = strchr(content, ':'); @@ -244,6 +254,12 @@ LanternYamlObject *lantern_yaml_read_array(const char *file_path, const char *ar fclose(fp); + if (!parse_error && in_target_array) { + if (commit_current(¤t, &objects, &capacity, out_count) != 0) { + parse_error = 1; + } + } + if (parse_error) { free_yaml_object(¤t); lantern_yaml_free_objects(objects, *out_count); @@ -254,13 +270,10 @@ LanternYamlObject *lantern_yaml_read_array(const char *file_path, const char *ar return NULL; } - if (in_target_array) { - commit_current(¤t, &objects, &capacity, out_count); - } - for (int i = 0; i < stack_size; ++i) { free(keys_stack[i]); } + free_yaml_object(¤t); if (*out_count == 0) { free(objects); diff --git a/src/networking/enr.c b/src/networking/enr.c index f3d14c8..5431808 100644 --- a/src/networking/enr.c +++ b/src/networking/enr.c @@ -232,15 +232,15 @@ int lantern_enr_record_decode(const char *enr_text, struct lantern_enr_record *r return -1; } + struct lantern_rlp_view root; + memset(&root, 0, sizeof(root)); + int root_ready = 0; uint8_t *encoded_bytes = NULL; size_t encoded_len = 0; if (lantern_base64url_decode(payload, &encoded_bytes, &encoded_len) != 0) { goto error; } - struct lantern_rlp_view root; - memset(&root, 0, sizeof(root)); - int root_ready = 0; if (lantern_rlp_decode(encoded_bytes, encoded_len, &root) != 0) { goto error; } @@ -356,6 +356,7 @@ int lantern_enr_record_build_v4( struct lantern_rlp_buffer signed_record = {0}; struct lantern_rlp_buffer content = {0}; struct lantern_rlp_buffer signature_buf = {0}; + size_t idx = 0; uint8_t ip_bytes[4]; if (parse_ipv4_address(ip_string, ip_bytes) != 0) { @@ -390,7 +391,6 @@ int lantern_enr_record_build_v4( } secp256k1_context_destroy(ctx); - size_t idx = 0; if (lantern_rlp_encode_uint64(&items[idx++], sequence) != 0) { error_reason = "rlp encode sequence failed"; goto error;