From 34ea68303c78d7baefae1ea29c19a8fbe174ef0b Mon Sep 17 00:00:00 2001 From: Nick Begg Date: Wed, 4 Mar 2026 14:56:15 +0100 Subject: [PATCH] lsof_free_result(): handle result == NULL Make lsof_free_result() consistent with lsof_destroy() (and free()) by accepting a NULL value. --- include/lsof.h | 4 ++++ lib/lsof.c | 3 +++ 2 files changed, 7 insertions(+) diff --git a/include/lsof.h b/include/lsof.h index 277e1af7..30ee16e6 100644 --- a/include/lsof.h +++ b/include/lsof.h @@ -572,6 +572,8 @@ enum lsof_error lsof_gather(struct lsof_context *ctx, * You should call `lsof_free_result` to free all `struct lsof_result` * before destroying the context. * + * ctx may be NULL. + * * You must not use the context anymore after this call. * * \since API version 1 @@ -579,6 +581,8 @@ enum lsof_error lsof_gather(struct lsof_context *ctx, void lsof_destroy(struct lsof_context *ctx); /** Free struct lsof_result + * + * result may be NULL. * * \since API version 1 */ diff --git a/lib/lsof.c b/lib/lsof.c index af4b04fd..cc60c7c4 100644 --- a/lib/lsof.c +++ b/lib/lsof.c @@ -732,6 +732,9 @@ void lsof_free_result(struct lsof_result *result) { int pi, fi; struct lsof_process *p; struct lsof_file *f; + if (!result) { + return; + } for (pi = 0; pi < result->num_processes; pi++) { p = &result->processes[pi]; /* Free files */