Fix #1565 - mg_snprintf %.*s with zero len printing

This commit is contained in:
Sergey Lyubka 2022-05-30 18:40:44 +01:00
parent b8bba7513a
commit e2fcf5f92c
3 changed files with 7 additions and 4 deletions

View File

@ -4918,7 +4918,7 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
size_t i = 0, n = 0;
while (fmt[i] != '\0') {
if (fmt[i] == '%') {
size_t j, k, x = 0, is_long = 0, w = 0 /* width */, pr = 0 /* prec */;
size_t j, k, x = 0, is_long = 0, w = 0 /* width */, pr = ~0U /* prec */;
char pad = ' ', minus = 0, c = fmt[++i];
if (c == '#') x++, c = fmt[++i];
if (c == '-') minus++, c = fmt[++i];
@ -4930,6 +4930,7 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
pr = (size_t) va_arg(ap, int);
c = fmt[++i];
} else {
pr = 0;
while (is_digit(c)) pr *= 10, pr += (size_t) (c - '0'), c = fmt[++i];
}
}
@ -4968,7 +4969,7 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
n++;
} else if (c == 's') {
char *p = va_arg(ap, char *);
if (pr == 0) pr = p == NULL ? 0 : strlen(p);
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
for (j = 0; !minus && pr < w && j + pr < w; j++)
n += mg_copys(buf, len, n, &pad, 1);
n += mg_copys(buf, len, n, p, pr);

View File

@ -286,7 +286,7 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
size_t i = 0, n = 0;
while (fmt[i] != '\0') {
if (fmt[i] == '%') {
size_t j, k, x = 0, is_long = 0, w = 0 /* width */, pr = 0 /* prec */;
size_t j, k, x = 0, is_long = 0, w = 0 /* width */, pr = ~0U /* prec */;
char pad = ' ', minus = 0, c = fmt[++i];
if (c == '#') x++, c = fmt[++i];
if (c == '-') minus++, c = fmt[++i];
@ -298,6 +298,7 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
pr = (size_t) va_arg(ap, int);
c = fmt[++i];
} else {
pr = 0;
while (is_digit(c)) pr *= 10, pr += (size_t) (c - '0'), c = fmt[++i];
}
}
@ -336,7 +337,7 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap) {
n++;
} else if (c == 's') {
char *p = va_arg(ap, char *);
if (pr == 0) pr = p == NULL ? 0 : strlen(p);
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
for (j = 0; !minus && pr < w && j + pr < w; j++)
n += mg_copys(buf, len, n, &pad, 1);
n += mg_copys(buf, len, n, p, pr);

View File

@ -1329,6 +1329,7 @@ static void test_str(void) {
ASSERT(sn("%d", 0));
ASSERT(sn("%d", 1));
ASSERT(sn("%d", -1));
ASSERT(sn("%.*s", 0, "ab"));
ASSERT(sn("%.*s", 1, "ab"));
ASSERT(sn("%.1s", "ab"));
ASSERT(sn("%.99s", "a"));