progress: store throughput display in a strbuf

Coverity noticed that we strncpy() into a fixed-size buffer without making sure that it actually ended up NUL-terminated. This is unlikely to be a bug in practice, since throughput strings rarely hit 32 characters, but it would be nice to clean it up. The most obvious way to do so is to add a NUL-terminator. But instead, this patch switches the fixed-size buffer out for a strbuf. At first glance this seems much less efficient, until we realize that filling in the fixed-size buffer is done by writing into a strbuf and copying the result! By writing straight to the buffer, we actually end up more efficient: 1. We avoid an extra copy of the bytes. 2. Rather than malloc/free each time progress is shown, we can strbuf_reset and use the same buffer each time. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 24, 2015 at 17:05 UTC 3131977de1476185209d4d56a0494f5b30ee5557
1 file changed +8 -10
progress.c
+8 -10
@@ -25,7 +25,7 @@ struct throughput {
25 unsigned int last_bytes[TP_IDX_MAX];
26 unsigned int last_misecs[TP_IDX_MAX];
27 unsigned int idx;
28 - char display[32];
28 + struct strbuf display;
29 };
30
31 struct progress {
@@ -98,7 +98,7 @@ static int display(struct progress *progress, unsigned n, const char *done)
98 }
99
100 progress->last_value = n;
101 - tp = (progress->throughput) ? progress->throughput->display : "";
101 + tp = (progress->throughput) ? progress->throughput->display.buf : "";
102 eol = done ? done : " \r";
103 if (progress->total) {
104 unsigned percent = n * 100 / progress->total;
@@ -129,6 +129,7 @@ static int display(struct progress *progress, unsigned n, const char *done)
129 static void throughput_string(struct strbuf *buf, off_t total,
130 unsigned int rate)
131 {
132 + strbuf_reset(buf);
133 strbuf_addstr(buf, ", ");
134 strbuf_humanise_bytes(buf, total);
135 strbuf_addstr(buf, " | ");
@@ -141,7 +142,6 @@ void display_throughput(struct progress *progress, off_t total)
142 struct throughput *tp;
143 uint64_t now_ns;
144 unsigned int misecs, count, rate;
144 - struct strbuf buf = STRBUF_INIT;
145
146 if (!progress)
147 return;
@@ -154,6 +154,7 @@ void display_throughput(struct progress *progress, off_t total)
154 if (tp) {
155 tp->prev_total = tp->curr_total = total;
156 tp->prev_ns = now_ns;
157 + strbuf_init(&tp->display, 0);
158 }
159 return;
160 }
@@ -193,9 +194,7 @@ void display_throughput(struct progress *progress, off_t total)
194 tp->last_misecs[tp->idx] = misecs;
195 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
196
196 - throughput_string(&buf, total, rate);
197 - strncpy(tp->display, buf.buf, sizeof(tp->display));
198 - strbuf_release(&buf);
197 + throughput_string(&tp->display, total, rate);
198 if (progress->last_value != -1 && progress_update)
199 display(progress, progress->last_value, NULL);
200 }
@@ -250,12 +249,9 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)
249
250 bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
251 if (tp) {
253 - struct strbuf strbuf = STRBUF_INIT;
252 unsigned int rate = !tp->avg_misecs ? 0 :
253 tp->avg_bytes / tp->avg_misecs;
256 - throughput_string(&strbuf, tp->curr_total, rate);
257 - strncpy(tp->display, strbuf.buf, sizeof(tp->display));
258 - strbuf_release(&strbuf);
254 + throughput_string(&tp->display, tp->curr_total, rate);
255 }
256 progress_update = 1;
257 sprintf(bufp, ", %s.\n", msg);
@@ -264,6 +260,8 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)
260 free(bufp);
261 }
262 clear_progress_signal();
263 + if (progress->throughput)
264 + strbuf_release(&progress->throughput->display);
265 free(progress->throughput);
266 free(progress);
267 }