From 649fe0fc8b9366375eab67639cab404617c527cd Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 14 Nov 2023 07:48:10 -1000 Subject: [PATCH] Fix handling of null strings with the s specifier --- include/fmt/format.h | 7 ++++--- test/format-test.cc | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 4104d91f..b84b6a5c 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2342,9 +2342,10 @@ template FMT_CONSTEXPR auto write(OutputIt out, const Char* s, const format_specs& specs, locale_ref) -> OutputIt { - return specs.type != presentation_type::pointer - ? write(out, basic_string_view(s), specs, {}) - : write_ptr(out, bit_cast(s), &specs); + if (specs.type == presentation_type::pointer) + return write_ptr(out, bit_cast(s), &specs); + if (!s) throw_format_error("string pointer is null"); + return write(out, basic_string_view(s), specs, {}); } template (nullptr); EXPECT_THROW_MSG( - (void)fmt::format(runtime("{0}"), static_cast(nullptr)), + (void)fmt::format("{}", nullstr), + format_error, "string pointer is null"); + EXPECT_THROW_MSG( + (void)fmt::format("{:s}", nullstr), format_error, "string pointer is null"); }