From: Aaron Miller Date: Fri, 9 Dec 2016 23:38:35 +0000 (-0800) Subject: Fix right trim() in JSON output X-Git-Tag: v1.1~9^2 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=6331b1466488d88a56e94d76a37852fbbc12d46d;p=users%2Fsagi%2Fnvme-cli.git 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. --- diff --git a/nvme-print.c b/nvme-print.c index c85e4860..620b421e 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--; } }