Skip to content

Commit 3131977

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0bb443f commit 3131977

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

progress.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct throughput {
2525
unsigned int last_bytes[TP_IDX_MAX];
2626
unsigned int last_misecs[TP_IDX_MAX];
2727
unsigned int idx;
28-
char display[32];
28+
struct strbuf display;
2929
};
3030

3131
struct progress {
@@ -98,7 +98,7 @@ static int display(struct progress *progress, unsigned n, const char *done)
9898
}
9999

100100
progress->last_value = n;
101-
tp = (progress->throughput) ? progress->throughput->display : "";
101+
tp = (progress->throughput) ? progress->throughput->display.buf : "";
102102
eol = done ? done : " \r";
103103
if (progress->total) {
104104
unsigned percent = n * 100 / progress->total;
@@ -129,6 +129,7 @@ static int display(struct progress *progress, unsigned n, const char *done)
129129
static void throughput_string(struct strbuf *buf, off_t total,
130130
unsigned int rate)
131131
{
132+
strbuf_reset(buf);
132133
strbuf_addstr(buf, ", ");
133134
strbuf_humanise_bytes(buf, total);
134135
strbuf_addstr(buf, " | ");
@@ -141,7 +142,6 @@ void display_throughput(struct progress *progress, off_t total)
141142
struct throughput *tp;
142143
uint64_t now_ns;
143144
unsigned int misecs, count, rate;
144-
struct strbuf buf = STRBUF_INIT;
145145

146146
if (!progress)
147147
return;
@@ -154,6 +154,7 @@ void display_throughput(struct progress *progress, off_t total)
154154
if (tp) {
155155
tp->prev_total = tp->curr_total = total;
156156
tp->prev_ns = now_ns;
157+
strbuf_init(&tp->display, 0);
157158
}
158159
return;
159160
}
@@ -193,9 +194,7 @@ void display_throughput(struct progress *progress, off_t total)
193194
tp->last_misecs[tp->idx] = misecs;
194195
tp->idx = (tp->idx + 1) % TP_IDX_MAX;
195196

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);
199198
if (progress->last_value != -1 && progress_update)
200199
display(progress, progress->last_value, NULL);
201200
}
@@ -250,12 +249,9 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)
250249

251250
bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
252251
if (tp) {
253-
struct strbuf strbuf = STRBUF_INIT;
254252
unsigned int rate = !tp->avg_misecs ? 0 :
255253
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);
259255
}
260256
progress_update = 1;
261257
sprintf(bufp, ", %s.\n", msg);
@@ -264,6 +260,8 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)
264260
free(bufp);
265261
}
266262
clear_progress_signal();
263+
if (progress->throughput)
264+
strbuf_release(&progress->throughput->display);
267265
free(progress->throughput);
268266
free(progress);
269267
}

0 commit comments

Comments
 (0)