eta: adjust truncation case
authorSitsofe Wheeler <sitsofe@yahoo.com>
Fri, 22 Dec 2017 11:10:35 +0000 (11:10 +0000)
committerSitsofe Wheeler <sitsofe@yahoo.com>
Sun, 24 Dec 2017 23:34:39 +0000 (23:34 +0000)
c076de85fa4901683f6ce23ecdc071c17801ffad ("eta: fix buffer overflow in
ETA output") made adjustments to cope with snprintf truncation. However
since we're replacing the null added by snprintf, left only needs to be
one smaller than the output buffer. Make this change and add a comment
about what we're doing.

Signed-off-by: Sitsofe Wheeler <sitsofe@yahoo.com>
eta.c

diff --git a/eta.c b/eta.c
index 087f57d965a015e171cd4478a2da4c5bdb5bdc4c..a156559fef5ba5607027a06cd16e4f051b685928 100644 (file)
--- a/eta.c
+++ b/eta.c
@@ -585,7 +585,7 @@ void display_thread_status(struct jobs_eta *je)
                        iops_str[ddir] = num2str(je->iops[ddir], 4, 1, 0, N2S_NONE);
                }
 
-               left = sizeof(output) - (p - output) - 2;
+               left = sizeof(output) - (p - output) - 1;
 
                if (je->rate[DDIR_TRIM] || je->iops[DDIR_TRIM])
                        l = snprintf(p, left,
@@ -601,8 +601,9 @@ void display_thread_status(struct jobs_eta *je)
                                rate_str[DDIR_READ], rate_str[DDIR_WRITE],
                                iops_str[DDIR_READ], iops_str[DDIR_WRITE],
                                eta_str);
-               if (l > left)
-                       l = left;
+               /* If truncation occurred adjust l so p is on the null */
+               if (l >= left)
+                       l = left - 1;
                p += l;
                if (l >= 0 && l < linelen_last)
                        p += sprintf(p, "%*s", linelen_last - l, "");