From de010f635d979053476cbb35d03231668ddd2ae8 Mon Sep 17 00:00:00 2001 From: hja Date: Sun, 15 Nov 2026 18:26:04 +0000 Subject: [PATCH 1/2] [darwin] Fix thread-list buffer sizing --- lib/dialects/darwin/dproc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dialects/darwin/dproc.c b/lib/dialects/darwin/dproc.c index 8b3ea2c3..c4b588d7 100644 --- a/lib/dialects/darwin/dproc.c +++ b/lib/dialects/darwin/dproc.c @@ -696,7 +696,7 @@ static void process_threads(struct lsof_context *ctx, /* context */ /* * Make sure a thread buffer has been allocated. */ - n += 10; + n = (n + 10) * sizeof(uint64_t); if (n > NbThreads) { while (n > NbThreads) { NbThreads += THREADS_INCR; From 5128c0ba86db14118c31d8f627e8fcac9d125b91 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Mon, 16 Nov 2026 09:04:06 +0800 Subject: [PATCH 2/2] [darwin] Use grow_array for the thread buffer Track the thread buffer capacity in entries (NThreads) instead of bytes, and grow it with grow_array(). This fixes the thread-count vs byte-size mismatch that truncated heavily threaded processes. --- lib/dialects/darwin/dlsof.h | 4 ++-- lib/dialects/darwin/dproc.c | 26 ++++++++------------------ 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/lib/dialects/darwin/dlsof.h b/lib/dialects/darwin/dlsof.h index 48ad413b..7f5841f0 100644 --- a/lib/dialects/darwin/dlsof.h +++ b/lib/dialects/darwin/dlsof.h @@ -136,7 +136,7 @@ struct lsof_context_dialect { int nb_fds; /* bytes allocated to FDs */ # if DARWINV >= 900 - int nb_threads; /* Threads bytes allocated */ + int n_threads; /* Threads entries allocated */ uint64_t *threads; /* Thread buffer */ # endif /* DARWINV>=900 */ @@ -161,7 +161,7 @@ struct lsof_context_dialect { # if DARWINV >= 900 # define Threads (ctx->dialect.threads) -# define NbThreads (ctx->dialect.nb_threads) +# define NThreads (ctx->dialect.n_threads) # endif /* DARWINV>=900 */ # if defined(PROC_PIDLISTFILEPORTS) diff --git a/lib/dialects/darwin/dproc.c b/lib/dialects/darwin/dproc.c index c4b588d7..3f4e90f1 100644 --- a/lib/dialects/darwin/dproc.c +++ b/lib/dialects/darwin/dproc.c @@ -46,10 +46,6 @@ static char copyright[] = "@(#) Copyright 2005-2007 Apple Inc. and Purdue " #define PIDS_INCR (sizeof(int) * 32) /* PID space increment */ #define VIPS_INCR 16 /* Vips space increment */ -#if DARWINV >= 900 -# define THREADS_INCR (sizeof(uint64_t) * 32) /* Threads space increment */ -#endif /* DARWINV>=900 */ - #if defined(PROC_PIDLISTFILEPORTS) # define FILEPORTS_INCR \ (sizeof(struct proc_fileportinfo) * 32) /* Fileports space increment \ @@ -692,30 +688,24 @@ static void process_threads(struct lsof_context *ctx, /* context */ int pid, /* PID */ uint32_t n) /* number of threads */ { - int i, nb, nt; + int i, nb, need, nt; /* * Make sure a thread buffer has been allocated. */ - n = (n + 10) * sizeof(uint64_t); - if (n > NbThreads) { - while (n > NbThreads) { - NbThreads += THREADS_INCR; - } - if (!Threads) - Threads = (uint64_t *)malloc((MALLOC_S)NbThreads); - else - Threads = - (uint64_t *)realloc((MALLOC_P *)Threads, (MALLOC_S)NbThreads); - if (!Threads) { + need = (int)n + 10; + if (need > NThreads) { + if (grow_array((void **)&Threads, &NThreads, sizeof(uint64_t), + need - NThreads)) { (void)fprintf(stderr, "%s: can't allocate space for %d Threads\n", - Pn, (int)(NbThreads / sizeof(int *))); + Pn, need); Error(ctx); } } /* * Get thread information for the process. */ - nb = proc_pidinfo(pid, PROC_PIDLISTTHREADS, 0, Threads, NbThreads); + nb = proc_pidinfo(pid, PROC_PIDLISTTHREADS, 0, Threads, + (int)(NThreads * sizeof(uint64_t))); if (nb <= 0) { if (errno == ESRCH) {