aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2025-01-31 09:43:30 +0000
committergingerBill <bill@gingerbill.org>2025-01-31 09:43:30 +0000
commita219da14ce8bbd15fec4ea962fd0ec175da0f0aa (patch)
tree82c29478732c8f8e89d9084a69021eeb8e6aa86f /src
parent05a2d1bfbf1387bc0cc53064f1635018034ae73a (diff)
Fix `gb.h`'s `gb_fprintf_va` to allocate if the string is larger than the default buffer
Diffstat (limited to 'src')
-rw-r--r--src/gb/gb.h13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/gb/gb.h b/src/gb/gb.h
index f74026c7d..59611ceb6 100644
--- a/src/gb/gb.h
+++ b/src/gb/gb.h
@@ -5837,9 +5837,20 @@ gb_inline isize gb_printf_err_va(char const *fmt, va_list va) {
}
gb_inline isize gb_fprintf_va(struct gbFile *f, char const *fmt, va_list va) {
- gb_local_persist char buf[4096];
+ char buf[4096];
isize len = gb_snprintf_va(buf, gb_size_of(buf), fmt, va);
+ char *new_buf = NULL;
+ isize n = gb_size_of(buf);
+ while (len < 0) {
+ n <<= 1;
+ gb_free(gb_heap_allocator(), new_buf);
+ new_buf = gb_alloc_array(gb_heap_allocator(), char, n);;
+ len = gb_snprintf_va(new_buf, n, fmt, va);
+ }
gb_file_write(f, buf, len-1); // NOTE(bill): prevent extra whitespace
+ if (new_buf != NULL) {
+ gb_free(gb_heap_allocator(), new_buf);
+ }
return len;
}