From 3661aab4cf7509b03f749477082553f4eee7dc0e Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 1 Aug 2019 11:43:58 +0200 Subject: dmesg: fix output hex encoding The current code ignores single-byte non-printable characters. Reported-by: Marc Deslauriers Signed-off-by: Karel Zak --- sys-utils/dmesg.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c index f0b9b7137..9f2e5b804 100644 --- a/sys-utils/dmesg.c +++ b/sys-utils/dmesg.c @@ -637,25 +637,29 @@ static void safe_fwrite(const char *buf, size_t size, int indent, FILE *out) if (len == (size_t)-1 || len == (size_t)-2) { /* invalid sequence */ memset(&s, 0, sizeof (s)); len = hex = 1; - } else if (len > 1 && !iswprint(wc)) { /* non-printable multibyte */ - hex = 1; - } - i += len - 1; -#else - len = 1; - if (!isprint((unsigned char) *p) && - !isspace((unsigned char) *p)) /* non-printable */ - hex = 1; + i += len - 1; + } else if (len > 1) { + if (!iswprint(wc) && !iswspace(wc)) /* non-printable multibyte */ + hex = 1; + i += len - 1; + } else #endif + { + len = 1; + if (!isprint((unsigned char) *p) && + !isspace((unsigned char) *p)) /* non-printable */ + hex = 1; + } + if (hex) rc = fwrite_hex(p, len, out); else if (*p == '\n' && *(p + 1) && indent) { rc = fwrite(p, 1, len, out) != len; if (fprintf(out, "%*s", indent, "") != indent) rc |= 1; - } - else + } else rc = fwrite(p, 1, len, out) != len; + if (rc != 0) { if (errno != EPIPE) err(EXIT_FAILURE, _("write failed")); -- cgit v1.2.3-55-g7522