Skip to content

Commit

Permalink
use va_arg instead of vsnprintf for calculate build string len
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed Nov 19, 2024
1 parent 18ebaf5 commit cf43e80
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/ccode/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,14 @@ char* build_string(char* format, ...) {
va_start(args, format);

/* Determine the size needed for the string */
int size = vsnprintf(NULL, 0, format, args) + 1;
int size = strlen(format);
const char *arg;

/* Count the number of arguments until we hit a NULL */
while ((arg = va_arg(args, const char *)) != NULL) {
size+= strlen(arg);
}

va_end(args);

/* Allocate memory for the string */
Expand All @@ -232,7 +239,7 @@ char* build_string(char* format, ...) {

/* Format the string */
va_start(args, format);
vsnprintf(result, size, format, args);
sprintf(result, format, args);
va_end(args);

return result;
Expand Down

0 comments on commit cf43e80

Please sign in to comment.