]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/mbsalign: encode \x to \xecx
authorKarel Zak <kzak@redhat.com>
Wed, 20 Dec 2017 12:01:43 +0000 (13:01 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 20 Dec 2017 12:01:43 +0000 (13:01 +0100)
Don't encode stand alone '\', our encoding is about \x<hex>, so we
need to care about \x prefix only.

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

index dce1a1321479e02747e99f204b61866bc0f345af..07fc313b845f4b163808dda41337422f9bed8f90 100644 (file)
@@ -54,7 +54,8 @@ size_t mbs_safe_nwidth(const char *buf, size_t bufsz, size_t *sz)
                last = p + (bufsz - 1);
 
        while (p && *p && p <= last) {
-               if (*p == '\\' || iscntrl((unsigned char) *p)) {
+               if ((p < last && *p == '\\' && *(p + 1) == 'x')
+                   || iscntrl((unsigned char) *p)) {
                        width += 4, bytes += 4;         /* *p encoded to \x?? */
                        p++;
                }
@@ -135,7 +136,8 @@ char *mbs_safe_encode_to_buffer(const char *s, size_t *width, char *buf, const c
                        continue;
                }
 
-               if (*p == '\\' || iscntrl((unsigned char) *p)) {
+               if ((*p == '\\' && *(p + 1) == 'x')
+                   || iscntrl((unsigned char) *p)) {
                        sprintf(r, "\\x%02x", (unsigned char) *p);
                        r += 4;
                        *width += 4;
@@ -239,6 +241,10 @@ char *mbs_invalid_encode_to_buffer(const char *s, size_t *width, char *buf)
                                (*width)++;
                                *r++ = *p;
                        }
+               } else if (*p == '\\' && *(p + 1) == 'x') {
+                       sprintf(r, "\\x%02x", (unsigned char) *p);
+                       r += 4;
+                       *width += 4;
                } else {
                        memcpy(r, p, len);
                        r += len;