Bug 11971 - ps output is run together when PID is too wide
Summary: ps output is run together when PID is too wide
Status: RESOLVED WORKSFORME
Alias: None
Product: Busybox
Classification: Unclassified
Component: Standard Compliance (show other bugs)
Version: 1.33.x
Hardware: All Linux
: P5 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-06-20 21:02 UTC by Jeff Barber
Modified: 2021-03-29 14:07 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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);
 }