From 7bb0a525c3ff895ad946bdbf4e408b7d8c7787a9 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 16 Nov 2023 00:00:26 +0100 Subject: [PATCH] Fixed text functions fast-path for handling "%s" and "%.*s" to handle null pointers gracefully. (#7016, #3466, #6846) --- docs/CHANGELOG.txt | 4 ++++ imgui.cpp | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index a09fb82ca..6658dcd4b 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -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) ----------------------------------------------------------------------- diff --git a/imgui.cpp b/imgui.cpp index 7ae276ede..0cbaa09fd 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1944,6 +1944,8 @@ 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); } } @@ -1951,6 +1953,11 @@ void ImFormatStringToTempBufferV(const char** out_buf, const char** out_buf_end, { 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. }