From 4fbf8d22a99445bc7b37c6d36ed9e045821439e3 Mon Sep 17 00:00:00 2001
From: Jim Warner <james.warner@comcast.net>
Date: Mon, 4 Apr 2022 00:00:00 -0500
Subject: [PATCH] ps: that restored aix behavior is now even more robust

With the commit referenced below a nasty bug affecting
aix parsing was swatted. Beyond the bug, the logic was
enhanced to disallow commas in the format string since
they would otherwise be shown with their field's data.

However, there remained many characters other than ','
that could survive the edits to then be shown adjacent
to the data. Well, with this patch they won't anymore!

[ along the way we will no longer try to forgive the ]
[ use of a double '%%' prefix since that resulted in ]
[ display of one '%' and field code instead of data. ]

Reference(s):
. March, 2022 - restored aix bahavior
commit 8cb646bdfc6e37b221f9616df96c6c116e6def28

Signed-off-by: Jim Warner <james.warner@comcast.net>
---
 ps/sortformat.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/ps/sortformat.c b/ps/sortformat.c
index 965219c2..bc90ca00 100644
--- a/ps/sortformat.c
+++ b/ps/sortformat.c
@@ -127,11 +127,11 @@ static const char *aix_format_parse(sf_node *sfn){
   items = 0;
   walk = sfn->sf;
   /* state machine */ {
-  int c;
+  int c = *walk++;
   initial:
-    c = *walk++;
     if(c=='%')    goto get_desc;
     if(!c)        goto looks_ok;
+    if(c==' ')    goto get_more;
     if(c==',')    goto aix_oops;
   /* get_text: */
     items++;
@@ -144,7 +144,8 @@ static const char *aix_format_parse(sf_node *sfn){
   get_desc:
     items++;
     c = *walk++;
-    if(c)         goto initial;
+    if(c&&c!=' ') goto initial;
+    return _("missing AIX field descriptor");
   aix_oops:
     return _("improper AIX field descriptor");
   looks_ok:
@@ -162,7 +163,8 @@ static const char *aix_format_parse(sf_node *sfn){
     if(*walk == '%'){
       const aix_struct *aix;
       walk++;
-      if(*walk == '%') goto double_percent;
+      if(*walk == '%')
+        return _("missing AIX field descriptor");
       aix = search_aix_array(*walk);
       walk++;
       if(!aix){
@@ -178,11 +180,6 @@ static const char *aix_format_parse(sf_node *sfn){
       size_t len;
       len = strcspn(walk, "%");
       memcpy(buf,walk,len);
-      if(0){
-double_percent:
-        len = 1;
-        buf[0] = '%';
-      }
       buf[len] = '\0';
       walk += len;
       fnode = xmalloc(sizeof(format_node));
-- 
2.40.0