Bug 11971

Summary: ps output is run together when PID is too wide
Product: Busybox Reporter: Jeff Barber <jsbarber60>
Component: Standard ComplianceAssignee: unassigned
Status: RESOLVED WORKSFORME    
Severity: normal CC: busybox-cvs
Priority: P5    
Version: 1.33.x   
Target Milestone: ---   
Hardware: All   
OS: Linux   
Host: Target:
Build:

Description Jeff Barber 2019-06-20 21:02:17 UTC
In the command "ps ax -o pid,args" the PID is run together with the next field whenever the PID is longer than 5 characters.

Easy demonstration with alpine linux in a docker container:

$ docker run -it alpine:latest
/ # for n in $(seq 100000); do (:) ; done
/ # ps ax -o pid,args
PID   COMMAND
    1 /bin/sh
100008ps ax -o pid,args


I also built the current master branch on my local ubuntu box and reproduced:
(Create processes to get PID above 100000, then...)

$ ./busybox ps ax -o pid,args
PID   COMMAND
...
102096busybox ps ax -o pid,args


Standard: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ps.html
...
-o  format
Write information according to the format specification given in format. This is fully described in the STDOUT section. Multiple -o options can be specified; the format specification shall be interpreted as the <space>-separated concatenation of all the format option-arguments.
Comment 1 Denys Vlasenko 2021-03-29 14:07:24 UTC
commit 3106784e654e7443ab724d927f9de0230adbe5ac
Author: Denys Vlasenko <vda.linux@googlemail.com>
Date:   Fri May 3 09:49:56 2019 +0200

    ps: ensure fields are separated by at least one space, closes 11826

    Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>

diff --git a/procps/ps.c b/procps/ps.c
index 54e6c40fc..815c11578 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -533,7 +533,7 @@ static void format_process(const procps_status_t *ps)
                len = out[i].width - len + 1;
                if (++i == out_cnt) /* do not pad last field */
                        break;
-               p += sprintf(p, "%*s", len, "");
+               p += sprintf(p, "%*s", len, " "); /* " ", not "", to ensure separation of fields */
        }
        printf("%.*s\n", terminal_width, buffer);
 }