Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

tools subcmd: Tighten the filename size in check_if_command_finished

FILENAME_MAX is often PATH_MAX (4kb), far more than needed for the
/proc path. Make the buffer size sufficient for the maximum integer
plus "/proc/" and "/status" with a '\0' terminator.

Fixes: 5ce42b5de461 ("tools subcmd: Add non-waitpid check_if_command_finished()")
Signed-off-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250717150855.1032526-1-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Ian Rogers and committed by
Namhyung Kim
478272d1 129f70bd

+13 -2
+13 -2
tools/lib/subcmd/run-command.c
··· 5 5 #include <ctype.h> 6 6 #include <fcntl.h> 7 7 #include <string.h> 8 + #include <linux/compiler.h> 8 9 #include <linux/string.h> 9 10 #include <errno.h> 10 11 #include <sys/wait.h> ··· 217 216 return result; 218 217 } 219 218 219 + /* 220 + * Conservative estimate of number of characaters needed to hold an a decoded 221 + * integer, assume each 3 bits needs a character byte and plus a possible sign 222 + * character. 223 + */ 224 + #ifndef is_signed_type 225 + #define is_signed_type(type) (((type)(-1)) < (type)1) 226 + #endif 227 + #define MAX_STRLEN_TYPE(type) (sizeof(type) * 8 / 3 + (is_signed_type(type) ? 1 : 0)) 228 + 220 229 int check_if_command_finished(struct child_process *cmd) 221 230 { 222 231 #ifdef __linux__ 223 - char filename[FILENAME_MAX + 12]; 232 + char filename[6 + MAX_STRLEN_TYPE(typeof(cmd->pid)) + 7 + 1]; 224 233 char status_line[256]; 225 234 FILE *status_file; 226 235 ··· 238 227 * Check by reading /proc/<pid>/status as calling waitpid causes 239 228 * stdout/stderr to be closed and data lost. 240 229 */ 241 - sprintf(filename, "/proc/%d/status", cmd->pid); 230 + sprintf(filename, "/proc/%u/status", cmd->pid); 242 231 status_file = fopen(filename, "r"); 243 232 if (status_file == NULL) { 244 233 /* Open failed assume finish_command was called. */