commit e8249b49ca3e02032dece5e0cdac3d236667a6d9
parent a570a80ed1606bed43118cb148fc83c3ac22b5c1
Author: runitclean <runitclean@disroot.org>
Date: Wed, 17 Dec 2025 12:39:17 +0100
pidof: fix -o option being applied without -o flag
The omit PID list was parsed even when -o was not specified. In
that case, arg was left uninitialized, so passing it to strtok()
resulted in undefined behavior and could lead to a segfault.
Only parse omit PIDs when -o is set.
Also fix the exit status to return 1 when no matching PID is found
Diffstat:
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/pidof.c b/pidof.c
@@ -55,13 +55,15 @@ main(int argc, char *argv[])
SLIST_INIT(&omitpid_head);
- for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
- pe = emalloc(sizeof(*pe));
- if (strcmp(p, "%PPID") == 0)
- pe->pid = getppid();
- else
- pe->pid = estrtol(p, 10);
- SLIST_INSERT_HEAD(&omitpid_head, pe, entry);
+ if (oflag) {
+ for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
+ pe = emalloc(sizeof(*pe));
+ if (strcmp(p, "%PPID") == 0)
+ pe->pid = getppid();
+ else
+ pe->pid = estrtol(p, 10);
+ SLIST_INSERT_HEAD(&omitpid_head, pe, entry);
+ }
}
if (!(dp = opendir("/proc")))
@@ -110,5 +112,5 @@ out:
closedir(dp);
- return 0;
+ return found ? 0 : 1;
}