Merge pull request #2921 from willisblackburn/master

Add terminating NUL after __func__ string literal #2920
This commit is contained in:
Bob Andrews
2026-02-07 16:34:01 +01:00
committed by GitHub
2 changed files with 21 additions and 0 deletions

View File

@@ -470,6 +470,7 @@ Literal* AddLiteral (const char* S)
{
StrBuf SB;
SB_InitFromString(&SB, S);
SB_AppendChar(&SB, '\0');
return AddLiteralStr(&SB);
}

20
test/val/bug2920.c Normal file
View File

@@ -0,0 +1,20 @@
#include "unittest.h"
TEST {
/* The bug causes __func__ to be not null terminated. */
const char *f = __func__;
size_t size = sizeof(__func__);
size_t i;
/* Ensure the size is correct (5 for "main" + null terminator) */
ASSERT_AreEqual((unsigned)size, 5u, "%u", "Sizeof __func__ should be 5");
/* Check content */
for (i = 0; i < size - 1; ++i) {
ASSERT_AreEqual(f[i], "main"[i], "%c", "Character mismatch");
}
/* Check null terminator specifically */
ASSERT_AreEqual(f[size - 1], '\0', "%02X", "Null terminator missing");
}
ENDTEST