From 4b60fac3a5c7a53f6d1a4480f37eb906bbacf21b Mon Sep 17 00:00:00 2001 From: Ed Maste Date: Fri, 11 Apr 2025 14:37:39 -0400 Subject: [PATCH] kinfo_getfile: Add a comment explaining 4/3 ratio We first get the expected size of the data from the kern.proc.filedesc sysctl, allocate 4/3 of that size, and then fetch the data. Add a comment about the reason (the fd table may grow between the two calls). Sponsored by: The FreeBSD Foundation --- lib/libutil/kinfo_getfile.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/libutil/kinfo_getfile.c b/lib/libutil/kinfo_getfile.c index f1441bdf771..345da657df8 100644 --- a/lib/libutil/kinfo_getfile.c +++ b/lib/libutil/kinfo_getfile.c @@ -27,6 +27,10 @@ kinfo_getfile(pid_t pid, int *cntp) error = sysctl(mib, nitems(mib), NULL, &len, NULL, 0); if (error) return (NULL); + /* + * Add extra space as the table may grow between requesting the size + * and fetching the data. + */ len = len * 4 / 3; buf = malloc(len); if (buf == NULL)