Skip to content

Commit 2c0f44c

Browse files
committed
fix resource leaks on error paths
1 parent 1b61a20 commit 2c0f44c

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

src/actions.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@ WOLFSENTRY_API wolfsentry_errcode_t wolfsentry_action_get_reference(WOLFSENTRY_C
240240
label_len = (int)strlen(label);
241241
if (label_len > WOLFSENTRY_MAX_LABEL_BYTES)
242242
WOLFSENTRY_ERROR_RETURN(STRING_ARG_TOO_LONG);
243+
WOLFSENTRY_SHARED_OR_RETURN();
243244
if ((action_template = (struct wolfsentry_action *)WOLFSENTRY_MALLOC(sizeof *action_template + (size_t)label_len)) == NULL)
244-
WOLFSENTRY_ERROR_RETURN(SYS_RESOURCE_FAILED);
245+
WOLFSENTRY_ERROR_UNLOCK_AND_RETURN(SYS_RESOURCE_FAILED);
245246
action_template->label_len = (byte)label_len;
246247
memcpy(action_template->label, label, (size_t)label_len);
247-
WOLFSENTRY_SHARED_OR_RETURN();
248248
ret = wolfsentry_action_get_reference_1(WOLFSENTRY_CONTEXT_ARGS_OUT, action_template, action);
249249
WOLFSENTRY_FREE(action_template);
250250
WOLFSENTRY_ERROR_UNLOCK_AND_RERETURN(ret);
@@ -470,7 +470,10 @@ WOLFSENTRY_LOCAL wolfsentry_errcode_t wolfsentry_action_list_clone(
470470

471471
new_ale->action = new_action;
472472
WOLFSENTRY_REFCOUNT_INCREMENT(new_action->header.refcount, ret);
473-
WOLFSENTRY_UNLOCK_AND_RERETURN_IF_ERROR(ret);
473+
if (ret < 0) {
474+
WOLFSENTRY_FREE_1(dest_context->hpi.allocator, new_ale);
475+
goto out;
476+
}
474477
wolfsentry_list_ent_append(&dest_action_list->header, &new_ale->header);
475478
}
476479
ret = WOLFSENTRY_ERROR_ENCODE(OK);

src/kv.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,12 @@ WOLFSENTRY_LOCAL wolfsentry_errcode_t wolfsentry_kv_delete(
536536
struct wolfsentry_kv_pair_internal *kv_template;
537537
struct wolfsentry_kv_pair_internal *old = NULL;
538538
wolfsentry_errcode_t ret;
539-
if ((ret = wolfsentry_kv_new(WOLFSENTRY_CONTEXT_ARGS_OUT, key, key_len, 0 /* data_len */, &kv_template)) < 0)
540-
WOLFSENTRY_ERROR_RERETURN(ret);
541539

542540
WOLFSENTRY_MUTEX_OR_RETURN();
543541

542+
if ((ret = wolfsentry_kv_new(WOLFSENTRY_CONTEXT_ARGS_OUT, key, key_len, 0 /* data_len */, &kv_template)) < 0)
543+
WOLFSENTRY_ERROR_UNLOCK_AND_RERETURN(ret);
544+
544545
ret = wolfsentry_kv_get_1(WOLFSENTRY_CONTEXT_ARGS_OUT, kv_table, kv_template, &old);
545546
WOLFSENTRY_WARN_ON_FAILURE(wolfsentry_kv_drop_reference(WOLFSENTRY_CONTEXT_ARGS_OUT, kv_template, NULL));
546547
WOLFSENTRY_UNLOCK_AND_RERETURN_IF_ERROR(ret);

src/routes.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,7 +2767,10 @@ static wolfsentry_errcode_t wolfsentry_route_event_dispatch_1(
27672767
if ((rule_route->parent_event == NULL) && (route_table->default_event != NULL)) {
27682768
rule_route->parent_event = route_table->default_event;
27692769
WOLFSENTRY_REFCOUNT_INCREMENT(rule_route->parent_event->header.refcount, ret);
2770-
WOLFSENTRY_UNLOCK_AND_RERETURN_IF_ERROR(ret);
2770+
if (ret < 0) {
2771+
rule_route->parent_event = NULL;
2772+
goto just_free_resources;
2773+
}
27712774
}
27722775
}
27732776

@@ -4719,11 +4722,14 @@ WOLFSENTRY_API wolfsentry_errcode_t wolfsentry_route_exports_render(WOLFSENTRY_C
47194722
ret = wolfsentry_addr_family_ntop(WOLFSENTRY_CONTEXT_ARGS_OUT, r->sa_family, &addr_family, &family_name);
47204723
if (WOLFSENTRY_ERROR_CODE_IS(ret, OK)) {
47214724
if (fprintf(f, ", AF = %s", family_name) < 0)
4722-
WOLFSENTRY_ERROR_RETURN(IO_FAILED);
4725+
ret = WOLFSENTRY_ERROR_ENCODE(IO_FAILED);
47234726
if (addr_family) {
4724-
if ((ret = wolfsentry_addr_family_drop_reference(WOLFSENTRY_CONTEXT_ARGS_OUT, addr_family, NULL /* action_results */ )) < 0)
4725-
WOLFSENTRY_ERROR_RERETURN(ret);
4727+
wolfsentry_errcode_t drop_ret = wolfsentry_addr_family_drop_reference(WOLFSENTRY_CONTEXT_ARGS_OUT, addr_family, NULL /* action_results */ );
4728+
if (drop_ret < 0)
4729+
WOLFSENTRY_ERROR_RERETURN(drop_ret);
47264730
}
4731+
if (ret < 0)
4732+
WOLFSENTRY_ERROR_RERETURN(ret);
47274733
} else
47284734
#endif
47294735
{

0 commit comments

Comments
 (0)