From 6331b1466488d88a56e94d76a37852fbbc12d46d Mon Sep 17 00:00:00 2001 From: Aaron Miller Date: Fri, 9 Dec 2016 15:38:35 -0800 Subject: [PATCH] Fix right trim() in JSON output Was always trimming from the end of the format buffer, not the end of the snprintf output. Also need to remove the initial decrement as snprintf will leave fmt_sz pointing at the terminating NUL. --- nvme-print.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nvme-print.c b/nvme-print.c index c85e486..620b421 100644 --- a/nvme-print.c +++ b/nvme-print.c @@ -63,14 +63,15 @@ void d_raw(unsigned char *buf, unsigned len) static void format(char *formatter, size_t fmt_sz, char *tofmt, size_t tofmtsz) { - snprintf(formatter,fmt_sz, "%-*.*s", + fmt_sz = snprintf(formatter,fmt_sz, "%-*.*s", (int)tofmtsz, (int)tofmtsz, tofmt); /* trim() the obnoxious trailing white lines */ - while (--fmt_sz) { + while (fmt_sz) { if (formatter[fmt_sz - 1] != ' ' && formatter[fmt_sz - 1] != '\0') { formatter[fmt_sz] = '\0'; break; } + fmt_sz--; } } -- 2.49.0