commit 6272b9a65812c9f653d693941b3317fe2f98a028
parent 89d3a0bd06d2345ffa98675e356a17be24a61210
Author: Quentin Rameau <quinq@fifth.space>
Date: Mon, 8 Nov 2021 23:44:06 +0100
Do not null-terminate strings printed by snprintf
Diffstat:
M | ui_ti.c | | | 42 | +++++++++++++++++++----------------------- |
M | ui_txt.c | | | 36 | +++++++++++++----------------------- |
2 files changed, 32 insertions(+), 46 deletions(-)
diff --git a/ui_ti.c b/ui_ti.c
@@ -75,9 +75,9 @@ uiprompt(char *fmt, ...)
putp(tparm(enter_standout_mode, 0, 0, 0, 0, 0, 0, 0, 0, 0));
va_start(ap, fmt);
- if (vsnprintf(bufout, sizeof(bufout), fmt, ap) >= sizeof(bufout))
- bufout[sizeof(bufout)-1] = '\0';
+ vsnprintf(bufout, sizeof(bufout), fmt, ap);
va_end(ap);
+
n = mbsprint(bufout, columns);
putp(tparm(exit_standout_mode, 0, 0, 0, 0, 0, 0, 0, 0, 0));
@@ -110,9 +110,9 @@ uiprompt(char *fmt, ...)
static void
printitem(Item *item)
{
- if (snprintf(bufout, sizeof(bufout), "%s %s", typedisplay(item->type),
- item->username) >= sizeof(bufout))
- bufout[sizeof(bufout)-1] = '\0';
+ snprintf(bufout, sizeof(bufout), "%s %s",
+ typedisplay(item->type), item->username);
+
mbsprint(bufout, columns);
putchar('\r');
}
@@ -163,13 +163,12 @@ uistatus(char *fmt, ...)
va_end(ap);
if (n < sizeof(bufout)-1) {
- n += snprintf(bufout + n, sizeof(bufout) - n,
- " [Press a key to continue \xe2\x98\x83]");
+ snprintf(bufout+n, sizeof(bufout)-n,
+ " [Press a key to continue \xe2\x98\x83]");
}
- if (n >= sizeof(bufout))
- bufout[sizeof(bufout)-1] = '\0';
- n = mbsprint(bufout, columns);
+ mbsprint(bufout, columns);
+
putp(tparm(exit_standout_mode, 0, 0, 0, 0, 0, 0, 0, 0, 0));
putp(tparm(clr_eol, 0, 0, 0, 0, 0, 0, 0, 0, 0));
@@ -184,22 +183,23 @@ displaystatus(Item *item)
{
Dir *dir = item->dat;
char *fmt;
- size_t n, nitems = dir ? dir->nitems : 0;
+ size_t nitems = dir ? dir->nitems : 0;
unsigned long long printoff = dir ? dir->printoff : 0;
putp(tparm(save_cursor, 0, 0, 0, 0, 0, 0, 0, 0, 0));
putp(tparm(cursor_address, lines-1, 0, 0, 0, 0, 0, 0, 0, 0));
putp(tparm(enter_standout_mode, 0, 0, 0, 0, 0, 0, 0, 0, 0));
+
fmt = (strcmp(item->port, "70") && strcmp(item->port, "gopher")) ?
"%1$3lld%%| %2$s:%5$s/%3$c%4$s" : "%3lld%%| %s/%c%s";
- if (snprintf(bufout, sizeof(bufout), fmt,
- (printoff + lines-1 >= nitems) ? 100 :
- (printoff + lines-1) * 100 / nitems,
- item->host, item->type, item->selector, item->port)
- >= sizeof(bufout))
- bufout[sizeof(bufout)-1] = '\0';
- n = mbsprint(bufout, columns);
+ snprintf(bufout, sizeof(bufout), fmt,
+ (printoff + lines-1 >= nitems) ? 100 :
+ (printoff + lines-1) * 100 / nitems,
+ item->host, item->type, item->selector, item->port);
+
+ mbsprint(bufout, columns);
+
putp(tparm(exit_standout_mode, 0, 0, 0, 0, 0, 0, 0, 0, 0));
putp(tparm(clr_eol, 0, 0, 0, 0, 0, 0, 0, 0, 0));
@@ -210,8 +210,6 @@ displaystatus(Item *item)
static void
displayuri(Item *item)
{
- size_t n;
-
if (item->type == 0 || item->type == 'i')
return;
@@ -222,10 +220,8 @@ displayuri(Item *item)
itemuri(item, bufout, sizeof(bufout));
- if (n >= sizeof(bufout))
- bufout[sizeof(bufout)-1] = '\0';
+ mbsprint(bufout, columns);
- n = mbsprint(bufout, columns);
putp(tparm(exit_standout_mode, 0, 0, 0, 0, 0, 0, 0, 0, 0));
putp(tparm(clr_eol, 0, 0, 0, 0, 0, 0, 0, 0, 0));
diff --git a/ui_txt.c b/ui_txt.c
@@ -78,11 +78,9 @@ uistatus(char *fmt, ...)
va_end(arg);
if (n < sizeof(bufout)-1) {
- n += snprintf(bufout + n, sizeof(bufout) - n,
- " [Press Enter to continue \xe2\x98\x83]");
+ snprintf(bufout+n, sizeof(bufout)-n,
+ " [Press Enter to continue \xe2\x98\x83]");
}
- if (n >= sizeof(bufout))
- bufout[sizeof(bufout)-1] = '\0';
mbsprint(bufout, columns);
fflush(stdout);
@@ -101,12 +99,11 @@ printstatus(Item *item, char c)
fmt = (strcmp(item->port, "70") && strcmp(item->port, "gopher")) ?
"%1$3lld%%%*2$3$c %4$s:%8$s/%5$c%6$s [%7$c]: " :
"%3lld%% %s/%c%s [%c]: ";
- if (snprintf(bufout, sizeof(bufout), fmt,
- (printoff + lines-1 >= nitems) ? 100 :
- (printoff + lines) * 100 / nitems,
- item->host, item->type, item->selector, c, item->port)
- >= sizeof(bufout))
- bufout[sizeof(bufout)-1] = '\0';
+ snprintf(bufout, sizeof(bufout), fmt,
+ (printoff + lines-1 >= nitems) ? 100 :
+ (printoff + lines) * 100 / nitems,
+ item->host, item->type, item->selector, c, item->port);
+
mbsprint(bufout, columns);
}
@@ -119,8 +116,7 @@ uiprompt(char *fmt, ...)
ssize_t r;
va_start(ap, fmt);
- if (vsnprintf(bufout, sizeof(bufout), fmt, ap) >= sizeof(bufout))
- bufout[sizeof(bufout)-1] = '\0';
+ vsnprintf(bufout, sizeof(bufout), fmt, ap);
va_end(ap);
mbsprint(bufout, columns);
@@ -158,11 +154,10 @@ uidisplay(Item *entry)
nd = ndigits(nitems);
for (i = dir->printoff; i < nitems && i < nlines; ++i) {
- if (snprintf(bufout, sizeof(bufout), "%*zu %s %s",
- nd, i+1, typedisplay(items[i].type),
- items[i].username)
- >= sizeof(bufout))
- bufout[sizeof(bufout)-1] = '\0';
+ snprintf(bufout, sizeof(bufout), "%*zu %s %s",
+ nd, i+1,typedisplay(items[i].type),
+ items[i].username);
+
mbsprint(bufout, columns);
putchar('\n');
}
@@ -173,15 +168,10 @@ uidisplay(Item *entry)
void
printuri(Item *item, size_t i)
{
- int n;
-
if (!item || item->type == 0 || item->type == 'i')
return;
- n = itemuri(item, bufout, sizeof(bufout));
-
- if (n >= sizeof(bufout))
- bufout[sizeof(bufout)-1] = '\0';
+ itemuri(item, bufout, sizeof(bufout));
mbsprint(bufout, columns);
putchar('\n');