Skip to content

Commit

Permalink
vsnprintf: Fix crash when _out is NULL.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkaradzic committed Dec 5, 2024
1 parent 4a1f0ab commit 32e578b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,8 @@ namespace bx

int32_t vsnprintf(char* _out, int32_t _max, const char* _format, va_list _argList)
{
if (0 < _max)
if ( 0 < _max
&& NULL != _out)
{
StaticMemoryBlockWriter writer(_out, uint32_t(_max) );

Expand Down
8 changes: 8 additions & 0 deletions tests/vsnprintf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ TEST_CASE("Truncated output buffer.", "[string][printf]")
REQUIRE(4 == bx::snprintf(buffer1, BX_COUNTOF(buffer1), "abvg") );
REQUIRE('\0' == buffer1[BX_COUNTOF(buffer1)-1]);

buffer1[0] = '\xfb'; // null destination
REQUIRE(4 == bx::snprintf(NULL, BX_COUNTOF(buffer1), "abvg") );
REQUIRE('\xfb' == buffer1[0]);

buffer1[0] = '\xbf'; // one byte destination
REQUIRE(4 == bx::snprintf(buffer1, 1, "abvg") );
REQUIRE('\0' == buffer1[0]);

char buffer7[7]; // truncate
REQUIRE(10 == bx::snprintf(NULL, 0, "Ten chars!") );
REQUIRE(10 == bx::snprintf(buffer7, BX_COUNTOF(buffer7), "Ten chars!") );
Expand Down

0 comments on commit 32e578b

Please sign in to comment.