Bugzilla – Attachment 3679 Details for
Bug 3534
probable underflow calculating display width of file name
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
different approach
bz3534-alloc.diff (text/plain), 4.88 KB, created by
Damien Miller
on 2023-02-17 16:29:28 AEDT
(
hide
)
Description:
different approach
Filename:
MIME Type:
Creator:
Damien Miller
Created:
2023-02-17 16:29:28 AEDT
Size:
4.88 KB
patch
obsolete
>diff --git a/progressmeter.c b/progressmeter.c >index f69f70662..95b91807c 100644 >--- a/progressmeter.c >+++ b/progressmeter.c >@@ -31,7 +31,8 @@ > > #include <errno.h> > #include <signal.h> >-#include <stdarg.h> >+#include <signal.h> >+#include <stdlib.h> > #include <stdio.h> > #include <string.h> > #include <time.h> >@@ -51,10 +52,6 @@ > /* determines whether we can output to the terminal */ > static int can_output(void); > >-/* formats and inserts the specified size into the given buffer */ >-static void format_size(char *, int, off_t); >-static void format_rate(char *, int, off_t); >- > /* window resizing */ > static void sig_winch(int); > static void setscreensize(void); >@@ -84,10 +81,11 @@ can_output(void) > return (getpgrp() == tcgetpgrp(STDOUT_FILENO)); > } > >-static void >-format_rate(char *buf, int size, off_t bytes) >+static const char * >+format_rate(off_t bytes) > { > int i; >+ static char buf[((sizeof(off_t) * 8 * 4 * 2) / 10) + 16]; > > bytes *= 100; > for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++) >@@ -96,30 +94,33 @@ format_rate(char *buf, int size, off_t bytes) > i++; > bytes = (bytes + 512) / 1024; > } >- snprintf(buf, size, "%3lld.%1lld%c%s", >+ snprintf(buf, sizeof(buf), "%3lld.%1lld%c%s", > (long long) (bytes + 5) / 100, > (long long) (bytes + 5) / 10 % 10, > unit[i], > i ? "B" : " "); >+ return buf; > } > >-static void >-format_size(char *buf, int size, off_t bytes) >+static const char * >+format_size(off_t bytes) > { > int i; >+ static char buf[((sizeof(off_t) * 8 * 4) / 10) + 16]; > > for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++) > bytes = (bytes + 512) / 1024; >- snprintf(buf, size, "%4lld%c%s", >+ snprintf(buf, sizeof(buf), "%4lld%c%s", > (long long) bytes, > unit[i], > i ? "B" : " "); >+ return buf; > } > > void > refresh_progress_meter(int force_update) > { >- char buf[MAX_WINSIZE + 1], buf2[MAX_WINSIZE + 1]; >+ char *buf = NULL, *obuf = NULL; > off_t transferred; > double elapsed, now; > int percent; >@@ -166,40 +167,27 @@ refresh_progress_meter(int force_update) > > last_update = now; > >- /* Don't bother if we can't even display the percentage complete */ >+ /* Don't bother if we can't even display the completion percentage */ > if (win_size < 4) > return; > > /* filename */ >- *buf = '\0'; > file_len = cols = win_size - 36; > if (file_len > 0) { >- snmprintf(buf, sizeof(buf), &cols, "%-*s", file_len, file); >+ asmprintf(&buf, INT_MAX, &cols, "%-*s", file_len, file); > /* If we used fewer columns than expected then pad */ >- if (cols < file_len) { >- snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), >- "%*s", file_len - cols, ""); >- } >+ if (cols < file_len) >+ xextendf(&buf, NULL, "%*s", file_len - cols, ""); > } >- > /* percent of transfer done */ > if (end_pos == 0 || cur_pos == end_pos) > percent = 100; > else > percent = ((float)cur_pos / end_pos) * 100; > >- snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), >- " %3d%% ", percent); >- >- /* amount transferred */ >- format_size(buf + strlen(buf), sizeof(buf) - strlen(buf), >- cur_pos); >- strlcat(buf, " ", sizeof(buf)); >- >- /* bandwidth usage */ >- format_rate(buf + strlen(buf), sizeof(buf) - strlen(buf), >- (off_t)bytes_per_second); >- strlcat(buf, "/s ", sizeof(buf)); >+ /* percent / amount transferred / bandwidth usage */ >+ xextendf(&buf, NULL, " %3d%% %s %s/s ", percent, format_size(cur_pos), >+ format_rate((off_t)bytes_per_second)); > > /* ETA */ > if (!transferred) >@@ -208,9 +196,9 @@ refresh_progress_meter(int force_update) > stalled = 0; > > if (stalled >= STALL_TIME) >- strlcat(buf, "- stalled -", sizeof(buf)); >+ xextendf(&buf, NULL, "- stalled -"); > else if (bytes_per_second == 0 && bytes_left) >- strlcat(buf, " --:-- ETA", sizeof(buf)); >+ xextendf(&buf, NULL, " --:-- ETA"); > else { > if (bytes_left > 0) > seconds = bytes_left / bytes_per_second; >@@ -222,24 +210,28 @@ refresh_progress_meter(int force_update) > minutes = seconds / 60; > seconds -= minutes * 60; > >- if (hours != 0) >- snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), >- "%d:%02d:%02d", hours, minutes, seconds); >- else >- snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), >- " %02d:%02d", minutes, seconds); >+ if (hours != 0) { >+ xextendf(&buf, NULL, "%d:%02d:%02d", >+ hours, minutes, seconds); >+ } else { >+ xextendf(&buf, NULL, " %02d:%02d", minutes, seconds); >+ } > > if (bytes_left > 0) >- strlcat(buf, " ETA", sizeof(buf)); >+ xextendf(&buf, NULL, " ETA"); > else >- strlcat(buf, " ", sizeof(buf)); >+ xextendf(&buf, NULL, " "); > } > > /* Finally, truncate string at window width */ >- buf2[0] = '\r'; > cols = win_size - 1; >- snmprintf(buf2 + 1, sizeof(buf2) - 1, &cols, "%s", buf); >- atomicio(vwrite, STDOUT_FILENO, buf2, strlen(buf2)); >+ asmprintf(&obuf, INT_MAX, &cols, " %s", buf); >+ if (obuf != NULL) { >+ *obuf = '\r'; /* must insert as asmprintf() would escape it */ >+ atomicio(vwrite, STDOUT_FILENO, obuf, strlen(obuf)); >+ } >+ free(buf); >+ free(obuf); > } > > /*ARGSUSED*/
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Flags:
dtucker
:
ok+
Actions:
View
|
Diff
Attachments on
bug 3534
:
3665
|
3666
|
3667
|
3670
|
3674
|
3675
|
3676
|
3679
|
3680