From 1023f62338a108c792e982ae30eeee48010bf117 Mon Sep 17 00:00:00 2001 From: reuk Date: Mon, 8 Jul 2024 13:09:27 +0100 Subject: [PATCH] XmlElement: Avoid potential undefined-init-order issues with juce_xmltextContentAttributeName This fixes potential crashes when this static object is accessed from the constructors of other objects with static storage duration. A concrete example of this could be seen when running the following on Linux: static inline const Typeface::Ptr face = Typeface::createSystemTypefaceFor (...); Here, 'face' is a static data member of some class. Creating a system typeface on Linux will parse an XML document of system typefaces, eventually accessing juce_xmltextContentAttributeName. --- modules/juce_core/xml/juce_XmlElement.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/modules/juce_core/xml/juce_XmlElement.cpp b/modules/juce_core/xml/juce_XmlElement.cpp index 84068c3312..e213e4d479 100644 --- a/modules/juce_core/xml/juce_XmlElement.cpp +++ b/modules/juce_core/xml/juce_XmlElement.cpp @@ -918,7 +918,11 @@ bool XmlElement::isTextElement() const noexcept return tagName.isEmpty(); } -static const String juce_xmltextContentAttributeName ("text"); +static const String& getJuceXmlTextContentAttributeName() +{ + static String result { "text" }; + return result; +} const String& XmlElement::getText() const noexcept { @@ -926,13 +930,13 @@ const String& XmlElement::getText() const noexcept // isn't actually a text element.. If this contains text sub-nodes, you // probably want to use getAllSubText instead. - return getStringAttribute (juce_xmltextContentAttributeName); + return getStringAttribute (getJuceXmlTextContentAttributeName()); } void XmlElement::setText (const String& newText) { if (isTextElement()) - setAttribute (juce_xmltextContentAttributeName, newText); + setAttribute (getJuceXmlTextContentAttributeName(), newText); else jassertfalse; // you can only change the text in a text element, not a normal one. } @@ -964,7 +968,7 @@ String XmlElement::getChildElementAllSubText (StringRef childTagName, const Stri XmlElement* XmlElement::createTextElement (const String& text) { auto e = new XmlElement ((int) 0); - e->setAttribute (juce_xmltextContentAttributeName, text); + e->setAttribute (getJuceXmlTextContentAttributeName(), text); return e; }