jcs
/wifi_da
/amendments
/29
util: Fix truncated termination in vsnprintf, from upstream
jcs made amendment 29 3 months ago
--- util.c Sat Jan 27 23:11:28 2024
+++ util.c Thu Sep 19 20:55:28 2024
@@ -1789,17 +1789,17 @@ static struct format {
int precision;
} default_format;
-int bounded_vfprintf(FILE *fp, const char *fmt, va_list arg);
+short bounded_vfprintf(FILE *fp, const char *fmt, va_list arg);
static int nullio(FILE *fp, int i);
-int
+short
bounded_vfprintf(FILE *fp, const char *fmt, va_list arg)
{
register int c, i, nwritten = 0;
register unsigned long n;
register char *s;
#define VFPRINTF_BUFLEN 512
- char buf[VFPRINTF_BUFLEN], *digits, *t;
+ static char buf[VFPRINTF_BUFLEN], *digits, *t;
struct format F;
for (c = *fmt; c; c = *++fmt) {
@@ -2075,7 +2075,7 @@ done:
return(nwritten);
}
-int
+short
snprintf(char *s, size_t size, const char *fmt, ...)
{
return(vsnprintf(s, size, fmt, __va(fmt)));
@@ -2087,20 +2087,32 @@ nullio(FILE *fp, int i)
return(EOF);
}
-int
+short
vsnprintf(char *s, size_t size, const char *fmt, void *p)
{
FILE f;
- int n;
+ short n;
+ unsigned char zb;
memset(&f, 0, sizeof(f));
f.refnum = -1;
- f.ptr = (unsigned char *) s;
+ f.ptr = (unsigned char *)s;
f.cnt = size;
f.proc = nullio;
f.dirty = 1;
- if ((n = bounded_vfprintf(&f, fmt, p)) >= 0)
- s[n] = 0;
+ if (size == 0)
+ zb = s[0];
+
+ if ((n = bounded_vfprintf(&f, fmt, p)) >= 0) {
+ if (n < size)
+ s[n] = 0;
+ else
+ s[size - 1] = 0;
+ }
+
+ if (size == 0)
+ s[0] = zb;
+
return(n);
}
--- util.h Fri Oct 20 19:25:42 2023
+++ util.h Thu Sep 19 20:55:51 2024
@@ -159,7 +159,7 @@ size_t strlcat(char *dst, const char *src, size_t dsiz
size_t strlcpy(char *dst, const char *src, size_t dsize);
char * strndup(const char *str, size_t maxlen);
char * strsep(char **stringp, const char *delim);
-int snprintf(char *s, size_t size, const char *fmt, ...);
-int vsnprintf(char *s, size_t size, const char *fmt, void *p);
+short snprintf(char *s, size_t size, const char *fmt, ...);
+short vsnprintf(char *s, size_t size, const char *fmt, void *p);
#endif
\ No newline at end of file