Skip to content

Commit

Permalink
Fixed text functions fast-path for handling "%s" and "%.*s" to handle…
Browse files Browse the repository at this point in the history
… null pointers gracefully. (#7016, #3466, #6846)
  • Loading branch information
ocornut committed Nov 15, 2023
1 parent 3d083db commit 7bb0a52
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Breaking changes:

Other changes:

- Misc: Fixed text functions fast-path for handling "%s" and "%.*s" to handle null pointers gracefully,
like most printf implementations. (#7016, #3466, #6846) [@codefrog2002]


-----------------------------------------------------------------------
VERSION 1.90.0 (Released 2023-11-15)
-----------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1944,13 +1944,20 @@ void ImFormatStringToTempBufferV(const char** out_buf, const char** out_buf_end,
if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0)
{
const char* buf = va_arg(args, const char*); // Skip formatting when using "%s"
if (buf == NULL)
buf = "(null)";
*out_buf = buf;
if (out_buf_end) { *out_buf_end = buf + strlen(buf); }
}
else if (fmt[0] == '%' && fmt[1] == '.' && fmt[2] == '*' && fmt[3] == 's' && fmt[4] == 0)
{
int buf_len = va_arg(args, int); // Skip formatting when using "%.*s"
const char* buf = va_arg(args, const char*);
if (buf == NULL)
{
buf = "(null)";
buf_len = ImMin(buf_len, 6);
}
*out_buf = buf;
*out_buf_end = buf + buf_len; // Disallow not passing 'out_buf_end' here. User is expected to use it.
}
Expand Down

0 comments on commit 7bb0a52

Please # to comment.