Skip to content

Commit

Permalink
Handle zero size strings in CreatePrintfString.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchatelet committed Dec 10, 2024
1 parent 3b23cdc commit bda8404
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/utils/list_cpu_features.c
Original file line number Diff line number Diff line change
@@ -148,13 +148,15 @@ 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();
// `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 non empty string node.
static Node* CreateString(const char* value) {
if (value == NULL || *value == '\0') internal_error();
return CreatePrintfString("%s", value);
}

0 comments on commit bda8404

Please sign in to comment.