commit 5f0c7ab87b3f809b8f4639eb5b6d64ca322a9897
parent f1379723656f81890de6e559669376dd49f4c913
Author: Andrew Kloet <andrew@kloet.net>
Date: Wed, 29 Apr 2026 11:24:42 -0400
pushf: O(1) buffer append and safety fixes
Avoid rescanning the entire buffer with strlcat/strlen
Handle miscreant servers sending too much data to keep c->eol safe
Diffstat:
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/cio.c b/cio.c
@@ -480,7 +480,9 @@ pushf(int cn, const char *fmt, ...)
c->eol[n++] = ' ';
va_start(vl, fmt);
s = c->eol + n;
- n += vsnprintf(s, LineLen - n - 1, fmt, vl);
+ int wrote = vsnprintf(s, LineLen - n - 1, fmt, vl);
+ if (wrote >= LineLen - n - 1) wrote = LineLen - n - 2;
+ n += (wrote < 0) ? 0 : wrote; /* failed to append to buffer */
va_end(vl);
if (logfp) {
fprintf(logfp, "%-12.12s\t%04d-%02d-%02dT%02d:%02d:%02dZ\t%s\n",
@@ -489,8 +491,9 @@ pushf(int cn, const char *fmt, ...)
gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, s);
fflush(logfp);
}
- strlcat(c->buf, "\n", c->sz);
- c->eol = c->buf + strlen(c->buf);
+ c->eol += n;
+ *c->eol++ = '\n';
+ *c->eol = '\0';
if (cn == ch && c->n == 0) {
char *p = c->eol - n - 1;