Skip to content

Commit

Permalink
Fix gb.h's gb_fprintf_va to allocate if the string is larger than…
Browse files Browse the repository at this point in the history
… the default buffer
  • Loading branch information
gingerBill committed Jan 31, 2025
1 parent 05a2d1b commit a219da1
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/gb/gb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit a219da1

Please sign in to comment.