From 6ecf8b179950aa8046cb505588d8cbe3221a3f09 Mon Sep 17 00:00:00 2001 From: hja Date: Sat, 28 Nov 2026 22:33:44 +0000 Subject: [PATCH] lib: reject NULL command selectors --- include/lsof.h | 4 ++-- lib/lsof.c | 2 +- tests/LTnull.c | 7 ++++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/lsof.h b/include/lsof.h index 0b3cf023..f1edcd3e 100644 --- a/include/lsof.h +++ b/include/lsof.h @@ -476,8 +476,8 @@ enum lsof_error lsof_logic_and(struct lsof_context *ctx); * * You can call this function multiple times to add more search conditions. * - * \return LSOF_ERROR_INVALID_ARGUMENT if ctx is NULL or frozen, command is - * both included and excluded, or command exceeds the system's supported length + * \return LSOF_ERROR_INVALID_ARGUMENT if ctx is NULL or frozen, command is NULL + * or both included and excluded, or command exceeds the system's supported length * * \since API version 1 */ diff --git a/lib/lsof.c b/lib/lsof.c index b856d1af..f019cd74 100644 --- a/lib/lsof.c +++ b/lib/lsof.c @@ -145,7 +145,7 @@ enum lsof_error lsof_select_process(struct lsof_context *ctx, char *command, char *cp; /* command copy */ MALLOC_S len; struct str_lst *lpt, *str; - if (!ctx || ctx->frozen) { + if (!ctx || !command || ctx->frozen) { return LSOF_ERROR_INVALID_ARGUMENT; } diff --git a/tests/LTnull.c b/tests/LTnull.c index d8428450..905d4d97 100644 --- a/tests/LTnull.c +++ b/tests/LTnull.c @@ -29,10 +29,15 @@ int main(void) { failed |= expect_invalid("gather(ctx, NULL)", lsof_gather(ctx, NULL)); failed |= expect_invalid("select_process_regex(ctx, NULL)", lsof_select_process_regex(ctx, NULL)); + failed |= expect_invalid("select_process(ctx, NULL, 0)", + lsof_select_process(ctx, NULL, 0)); + failed |= expect_invalid("select_process(ctx, NULL, 1)", + lsof_select_process(ctx, NULL, 1)); /* Rejected arguments must not freeze or otherwise invalidate the context. */ if (lsof_select_pid(ctx, 1, 0) != LSOF_SUCCESS || lsof_select_pgid(ctx, 1, 0) != LSOF_SUCCESS || - lsof_select_process_regex(ctx, "/test/") != LSOF_SUCCESS) { + lsof_select_process_regex(ctx, "/test/") != LSOF_SUCCESS || + lsof_select_process(ctx, "test", 0) != LSOF_SUCCESS) { fprintf(stderr, "context is unusable after rejected arguments\n"); failed = 1; }