From 7fdab09bca8ccb8323b262efb739b93047b30cd8 Mon Sep 17 00:00:00 2001 From: Guillaume Chatelet Date: Tue, 10 Dec 2024 10:32:52 +0000 Subject: [PATCH] Fix allocator bump in printf string allocation Fixes #373. --- src/utils/list_cpu_features.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/utils/list_cpu_features.c b/src/utils/list_cpu_features.c index a173f866..ee8875e3 100644 --- a/src/utils/list_cpu_features.c +++ b/src/utils/list_cpu_features.c @@ -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.