Skip to content

Commit 2956122

Browse files
committed
Handle malloc failure in lsof_gather
1 parent bef56be commit 2956122

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

lib/lsof.c

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ enum lsof_error lsof_gather(struct lsof_context *ctx,
269269
int i;
270270
struct lsof_selection *selections;
271271
size_t num_selections = 0;
272+
struct lsof_process *user_procs = NULL;
273+
struct lsof_result *res = NULL;
272274

273275
if (!result) {
274276
ret = LSOF_ERROR_INVALID_ARGUMENT;
@@ -298,10 +300,20 @@ enum lsof_error lsof_gather(struct lsof_context *ctx,
298300
}
299301

300302
/* Fill result */
301-
struct lsof_result *res =
302-
(struct lsof_result *)malloc(sizeof(struct lsof_result));
303-
struct lsof_process *user_procs =
303+
res = (struct lsof_result *)malloc(sizeof(struct lsof_result));
304+
if (!res) {
305+
ret = LSOF_ERROR_NO_MEMORY;
306+
goto cleanup;
307+
}
308+
memset(res, 0, sizeof(struct lsof_result));
309+
310+
user_procs =
304311
(struct lsof_process *)malloc(sizeof(struct lsof_process) * sel_procs);
312+
if (!user_procs) {
313+
ret = LSOF_ERROR_NO_MEMORY;
314+
goto cleanup;
315+
}
316+
305317
memset(user_procs, 0, sizeof(struct lsof_process) * sel_procs);
306318

307319
for (pi = 0, upi = 0; pi < ctx->procs_size; pi++) {
@@ -343,6 +355,10 @@ enum lsof_error lsof_gather(struct lsof_context *ctx,
343355

344356
p->files = (struct lsof_file *)malloc(sizeof(struct lsof_file) *
345357
num_files);
358+
if (!p->files) {
359+
ret = LSOF_ERROR_NO_MEMORY;
360+
goto cleanup;
361+
}
346362
memset(p->files, 0, sizeof(struct lsof_file) * num_files);
347363
p->num_files = num_files;
348364
for (fi = 0, lf = lp->file; lf; lf = lf_next) {
@@ -429,6 +445,7 @@ enum lsof_error lsof_gather(struct lsof_context *ctx,
429445
ctx->cur_proc = NULL;
430446

431447
res->processes = user_procs;
448+
user_procs = NULL;
432449
res->num_processes = sel_procs;
433450

434451
ctx->procs_size = ctx->procs_cap = 0;
@@ -652,6 +669,10 @@ enum lsof_error lsof_gather(struct lsof_context *ctx,
652669
if (pass == 0) {
653670
selections = (struct lsof_selection *)malloc(
654671
sizeof(struct lsof_selection) * num_selections);
672+
if (!selections) {
673+
ret = LSOF_ERROR_NO_MEMORY;
674+
goto cleanup;
675+
}
655676
memset(selections, 0,
656677
sizeof(struct lsof_selection) * num_selections);
657678
res->selections = selections;
@@ -661,7 +682,21 @@ enum lsof_error lsof_gather(struct lsof_context *ctx,
661682

662683
/* Params */
663684
*result = res;
685+
res = NULL;
686+
ret = LSOF_SUCCESS;
664687

688+
cleanup:
689+
/* Cleanup all allocated resources on failure */
690+
if (user_procs) {
691+
for (int j = 0; j < upi; j++) {
692+
CLEAN(user_procs[j].files);
693+
}
694+
CLEAN(user_procs);
695+
}
696+
if (res) {
697+
CLEAN(res->selections);
698+
CLEAN(res);
699+
}
665700
return ret;
666701
}
667702

0 commit comments

Comments
 (0)