]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
mbsalign: cleanup escape sequences handling
authorKarel Zak <kzak@redhat.com>
Wed, 18 Mar 2026 11:48:54 +0000 (12:48 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 18 Mar 2026 11:48:54 +0000 (12:48 +0100)
Use '\033' (portable) instead of '\e' (GNU extension), simplify
the control character handling by checking for ESC directly, and
fix the SCS boundary check to only match complete sequences.

Signed-off-by: Karel Zak <kzak@redhat.com>
lib/mbsalign.c

index 3e79ad10b4e8e8476a607cb44b1317bd541c539a..3a1385fec027e6f1e8cd9b30712b390365b94415 100644 (file)
@@ -42,14 +42,9 @@ size_t mbs_nwidth(const char *buf, size_t bufsz)
                last = p + (bufsz - 1);
 
        while (p && *p && p <= last) {
-               if (iscntrl((unsigned char) *p)) {
-                       char ctrl_char = *p;
+               if (*p == '\033') {
                        p++;
 
-                       /* only process escape sequences if the control char is ESC */
-                       if (ctrl_char != '\e')
-                               continue;
-
                        /* try detect "\e[x;ym" and skip on success */
                        if (*p && *p == '[') {
                                const char *e = p;
@@ -59,11 +54,12 @@ size_t mbs_nwidth(const char *buf, size_t bufsz)
                                        p = e + 1;
                        }
                        /* try detect SCS sequences "\e(X", "\e)X", "\e*X", "\e+X" and skip on success */
-                       else if (*p && (*p == '(' || *p == ')' || *p == '*' || *p == '+')) {
-                               p++;  /* skip the SCS introducer */
-                               if (p <= last)
-                                       p++;  /* skip the character */
-                       }
+                       else if (p < last && (*p == '(' || *p == ')' || *p == '*' || *p == '+'))
+                               p += 2;  /* skip the SCS sequence */
+                       continue;
+               }
+               if (iscntrl((unsigned char) *p)) {
+                       p++;
                        continue;
                }
 #ifdef HAVE_WIDECHAR