Skip to content

Commit

Permalink
Fix allocator bump in printf string allocation
Browse files Browse the repository at this point in the history
Fixes #373.
  • Loading branch information
gchatelet committed Dec 16, 2024
1 parent 744e451 commit a2b9437
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/utils/list_cpu_features.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ static Node* CreatePrintfString(const char* format, ...) {
const int written = vsnprintf(ptr, gBumpAllocator.size, format, arglist);
va_end(arglist);
if (written < 0 || written >= (int)gBumpAllocator.size) internal_error();
return CreateConstantString((char*)BA_Bump(written));
// `vsnprintf` does not set `\0` when no characters are to be written.
if (written == 0) gBumpAllocator.ptr = '\0';
// `vsnprintf` returns the number of printed characters excluding `\0`.
const int null_terminated_written = written + 1;
return CreateConstantString((char*)BA_Bump(null_terminated_written));
}

// Adds a string node.
Expand Down

0 comments on commit a2b9437

Please sign in to comment.