From 8d8104fd8bc4ebc7e7a0bbc0ea02971b79538a79 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 7 May 2025 22:10:16 +0100 Subject: [PATCH] Fix #14234, 5aa4ad5135: Crash due to unexpected NUL char in string. Gracefully handle an unexpected NUL character in non-debug builds. --- src/strings.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/strings.cpp b/src/strings.cpp index f6df675b28..12bf512a8b 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -1113,6 +1113,11 @@ static void FormatString(StringBuilder &builder, std::string_view str_arg, Strin const uint case_index = str_stack.top().case_index; char32_t b = consumer.ReadUtf8(); assert(b != 0); + if (b == 0) { + /* A NUL character should never be encountered, but for non-debug builds handle it gracefully. */ + builder += "(unexpected NUL)"; + continue; + } if (SCC_NEWGRF_FIRST <= b && b <= SCC_NEWGRF_LAST) { /* We need to pass some stuff as it might be modified. */