From 0a5be41f8af2b0211e3e5e788c34ca804c7dff77 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 4 Apr 2021 14:55:26 +0200 Subject: [PATCH] Avoid using uninitialized static wxString in wxOSX code Global "sCR" and "sLF" string objects could have been not yet initialized when wxMacConvertNewlines{13To10,10To13}() were called as these functions can be (implicitly) used when initializing other static objects. Fix the problem by avoiding the use of these objects, as well of wxString::Replace(), entirely and just iterating over the string directly using a simple, and more efficient, for loop. Note the use of "auto&&": a more usual "auto&" can't be used with temporary wxUniCharRef created by dereferencing wxString iterators. Using just "auto" would have actually worked too, but modifying a value via a copy would seem surprising, so use "auto&&" as a hopefully slightly more clear alternative. Closes #19110. --- src/osx/core/cfstring.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/osx/core/cfstring.cpp b/src/osx/core/cfstring.cpp index 38356233eb..c0c0bccd42 100644 --- a/src/osx/core/cfstring.cpp +++ b/src/osx/core/cfstring.cpp @@ -25,20 +25,25 @@ #include #include -const wxString sCR((wxChar)13); -const wxString sLF((wxChar)10); - wxString wxMacConvertNewlines13To10(const wxString& data) { wxString string(data); - string.Replace(sCR, sLF); + for (auto&& c: string) + { + if (c == '\r') + c = '\n'; + } return string; } wxString wxMacConvertNewlines10To13(const wxString& data) { wxString string(data); - string.Replace(sLF, sCR); + for (auto&& c: string) + { + if (c == '\n') + c = '\r'; + } return string; }