From 234c7010b636a237f1ed09fffd200d698e04054b Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Wed, 24 Jul 2024 02:16:05 +0200 Subject: [PATCH] compiler: fix formatting of large messages --- src/gb/gb.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/gb/gb.h b/src/gb/gb.h index 38dabc9bd98..384c481c39e 100644 --- a/src/gb/gb.h +++ b/src/gb/gb.h @@ -5882,7 +5882,7 @@ gb_internal isize gb__print_string(char *text, isize max_len, gbprivFmtInfo *inf len = info->precision < len ? info->precision : len; } - res += gb_strlcpy(text, str, len); + res += gb_strlcpy(text, str, gb_min(len, max_len)); if (info->width > res) { isize padding = info->width - len; @@ -6037,14 +6037,16 @@ gb_internal isize gb__print_f64(char *text, isize max_len, gbprivFmtInfo *info, gb_no_inline isize gb_snprintf_va(char *text, isize max_len, char const *fmt, va_list va) { char const *text_begin = text; isize remaining = max_len, res; + remaining--; - while (*fmt) { + while (*fmt && remaining) { gbprivFmtInfo info = {0}; isize len = 0; info.precision = -1; while (*fmt && *fmt != '%' && remaining) { *text++ = *fmt++; + remaining--; } if (*fmt == '%') { @@ -6210,7 +6212,7 @@ gb_no_inline isize gb_snprintf_va(char *text, isize max_len, char const *fmt, va text += len; if (len >= remaining) { - remaining = gb_min(remaining, 1); + remaining = 0; } else { remaining -= len; } @@ -6218,7 +6220,7 @@ gb_no_inline isize gb_snprintf_va(char *text, isize max_len, char const *fmt, va *text++ = '\0'; res = (text - text_begin); - return (res >= max_len || res < 0) ? -1 : res; + return (res > max_len || res < 0) ? -1 : res; }