]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: (pidutils) improve 'PID:inode' parsing logic
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Wed, 14 Jan 2026 14:51:58 +0000 (09:51 -0500)
committerChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Fri, 23 Jan 2026 18:52:21 +0000 (13:52 -0500)
The parsing logic in ul_parse_pid_str() failed to
identify invalid input when *pidstr starts with a
colon ':' followed by a number (inode), and also
in cases where the input does not consist of any
digits. And lastly if the PID or inode is 0.

Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
lib/pidutils.c

index a7567eb3fd4c995e49dd32b90fcfafb71a3f8dd1..3998816d6b97afcf801d998cd6c3e649d8809228 100644 (file)
@@ -21,7 +21,7 @@
  * If @pfd_ino is not destined to be set, pass it as NULL.
  *
  * Return: On success, 0 is returned.
- *         On failure, a negative errno number will be returned.
+ *         On failure, -1 is returned and errno is set to indicate the issue.
  */
 int ul_parse_pid_str(char *pidstr, pid_t *pid_num, uint64_t *pfd_ino)
 {
@@ -29,21 +29,36 @@ int ul_parse_pid_str(char *pidstr, pid_t *pid_num, uint64_t *pfd_ino)
        char *end = NULL;
        int64_t num = 0;
 
-       if (!pidstr || !*pidstr || !pid_num)
-               return -EINVAL;
+       if (!pidstr || !*pidstr || !pid_num) {
+               errno = EINVAL;
+               return -1;
+       }
 
        num = strtoimax(pidstr, &end, 10);
-       if (errno == 0 && ((num && num < 1) || (num && num > SINT_MAX(pid_t))))
-               return -ERANGE;
+       if (num == 0 && end == pidstr) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       if (errno == ERANGE || (num <= 0 || num > SINT_MAX(pid_t))) {
+               errno = ERANGE;
+               return -1;
+       }
+
        *pid_num = (pid_t) num;
 
        if (*end == ':' && pfd_ino) {
                rc = ul_strtou64(++end, pfd_ino, 10);
-               if (rc != 0)
-                       return -ERANGE;
+               if (rc != 0 || *pfd_ino == 0) {
+                       errno = EINVAL;
+                       return -1;
+               }
                *end = '\0';
        }
-       if (errno != 0 || ((end && *end != '\0') || pidstr >= end))
-               return -EINVAL;
+
+       if (end && *end != '\0') {
+               errno = EINVAL;
+               return -1;
+       }
        return 0;
 }