From 5dcf61a691fd2bcc02eedbcb523680bd2356b8c3 Mon Sep 17 00:00:00 2001 From: Tokunori Ikegami Date: Wed, 1 Nov 2023 08:14:35 +0900 Subject: [PATCH] nvme-print: Correct to print correct ascii character string length Signed-off-by: Tokunori Ikegami --- nvme-print.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/nvme-print.c b/nvme-print.c index 41bfec0c..72a9f6aa 100644 --- a/nvme-print.c +++ b/nvme-print.c @@ -382,15 +382,14 @@ void nvme_show_relatives(const char *name) void d(unsigned char *buf, int len, int width, int group) { - int i, offset = 0, line_done = 0; - char ascii[32 + 1]; + int i, offset = 0; + char ascii[32 + 1] = { 0 }; assert(width < sizeof(ascii)); printf(" "); for (i = 0; i <= 15; i++) printf("%3x", i); for (i = 0; i < len; i++) { - line_done = 0; if (i % width == 0) printf( "\n%04x:", offset); if (i % group == 0) @@ -399,18 +398,14 @@ void d(unsigned char *buf, int len, int width, int group) printf( "%02x", buf[i]); ascii[i % width] = (buf[i] >= '!' && buf[i] <= '~') ? buf[i] : '.'; if (((i + 1) % width) == 0) { - ascii[i % width + 1] = '\0'; printf( " \"%.*s\"", width, ascii); offset += width; - line_done = 1; + memset(ascii, 0, sizeof(ascii)); } } - if (!line_done) { + if (strlen(ascii)) { unsigned b = width - (i % width); - ascii[i % width + 1] = '\0'; - printf( " %*s \"%.*s\"", - 2 * b + b / group + (b % group ? 1 : 0), "", - width, ascii); + printf( " %*s \"%.*s\"", 2 * b + b / group + (b % group ? 1 : 0), "", width, ascii); } printf( "\n"); } -- 2.50.1