fix: replace unsafe fixed-size buffers with dynamic formatting helpers; add util format helpers; centralize log_on_line cleanup
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
#include "util.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char* format_string_va(const char* fmt, va_list args) {
|
||||
if (!fmt) return NULL;
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
int needed = vsnprintf(NULL, 0, fmt, args_copy);
|
||||
va_end(args_copy);
|
||||
if (needed < 0) return NULL;
|
||||
|
||||
char* buf = (char*)malloc((size_t)needed + 1);
|
||||
if (!buf) return NULL;
|
||||
vsnprintf(buf, (size_t)needed + 1, fmt, args);
|
||||
return buf;
|
||||
}
|
||||
|
||||
char* format_string(const char* fmt, ...) {
|
||||
if (!fmt) return NULL;
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
char* s = format_string_va(fmt, args);
|
||||
va_end(args);
|
||||
return s;
|
||||
}
|
||||
Reference in New Issue
Block a user