Skip to content

vsnprintf does not always nullterminate #266

Closed
@JannesN

Description

@JannesN

Consider this code:
char NumStr[32]; int Round = 116; bx::snprintf(NumStr, 32, "@[s:GUIText|a:000000FF]Round %i", Round);

In this case the buffer fits exactly the string WITHOUT the null terminator!
In this special case the resulting string is not null terminated.

This is the writing code in vsnprintf:
`StaticMemoryBlockWriter writer(_out, uint32_t(_max) );

		Error err;
		va_list argListCopy;
		va_copy(argListCopy, _argList);
		int32_t size = write(&writer, _format, argListCopy, &err);
		va_end(argListCopy);

		if (err.isOk() )
		{
			size += write(&writer, '\0', &err);
			return size - 1 /* size without '\0' terminator */;
		}
		else
		{
			_out[_max-1] = '\0';
		}`

In order for it to work correctly the writing of the null terminator has to be moved out of the if statement. Since err.isOk() will be true when the buffer was fully written!

On top of that it looks a bit to me like a return statement is missing in the else part?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions