From bed6074531e044dd49760f5004a92229dc13e3e7 Mon Sep 17 00:00:00 2001 From: Craig Small Date: Tue, 22 Dec 2020 17:50:17 +1100 Subject: [PATCH] ps: Stop crash if using test fields I'm not sure if anyone actually uses these things, but if you selected test fields on the command line ps would crash. $ ps/pscommand -o _left Signal 11 (SEGV) caught by pscommand (3.3.11.877-0488). /home/csmall/Projects/procps/procps/ps/.libs/pscommand:ps/display.c:66: please report this bug Segmentation fault Anyway, it doesn't now: $ ps/pscommand -o pid,_left,_left2,_right,_unlimited 1 PID LLLLLLLL L2L2L2L2 RRRRRRRRRRR U 1 tty7 3270/tty4 59:59 [123456789-12345] --- ps/output.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ps/output.c b/ps/output.c index 20f9a534..cbb0e6e2 100644 --- a/ps/output.c +++ b/ps/output.c @@ -1415,12 +1415,14 @@ setREL1(ID_TGID) // like "args" static int pr_t_unlimited(char *restrict const outbuf, const proc_t *restrict const pp){ static const char *const vals[] = {"[123456789-12345] ","ps","123456789-123456"}; + if (!outbuf) return 0; (void)pp; snprintf(outbuf, max_rightward+1, "%s", vals[lines_to_next_header%3u]); return strlen(outbuf); } static int pr_t_unlimited2(char *restrict const outbuf, const proc_t *restrict const pp){ static const char *const vals[] = {"unlimited", "[123456789-12345] ","ps","123456789-123456"}; + if (!outbuf) return 0; (void)pp; snprintf(outbuf, max_rightward+1, "%s", vals[lines_to_next_header%4u]); return strlen(outbuf); @@ -1429,11 +1431,13 @@ static int pr_t_unlimited2(char *restrict const outbuf, const proc_t *restrict c // like "etime" static int pr_t_right(char *restrict const outbuf, const proc_t *restrict const pp){ static const char *const vals[] = {"999-23:59:59","99-23:59:59","9-23:59:59","59:59"}; + if (!outbuf) return 0; (void)pp; return snprintf(outbuf, COLWID, "%s", vals[lines_to_next_header%4u]); } static int pr_t_right2(char *restrict const outbuf, const proc_t *restrict const pp){ static const char *const vals[] = {"999-23:59:59","99-23:59:59","9-23:59:59"}; + if (!outbuf) return 0; (void)pp; return snprintf(outbuf, COLWID, "%s", vals[lines_to_next_header%3u]); } @@ -1441,11 +1445,13 @@ static int pr_t_right2(char *restrict const outbuf, const proc_t *restrict const // like "tty" static int pr_t_left(char *restrict const outbuf, const proc_t *restrict const pp){ static const char *const vals[] = {"tty7","pts/9999","iseries/vtty42","ttySMX0","3270/tty4"}; + if (!outbuf) return 0; (void)pp; return snprintf(outbuf, COLWID, "%s", vals[lines_to_next_header%5u]); } static int pr_t_left2(char *restrict const outbuf, const proc_t *restrict const pp){ static const char *const vals[] = {"tty7","pts/9999","ttySMX0","3270/tty4"}; + if (!outbuf) return 0; (void)pp; return snprintf(outbuf, COLWID, "%s", vals[lines_to_next_header%4u]); }