From 8345fb0eab4e9cb8f5a1a0192158e6426e649349 Mon Sep 17 00:00:00 2001 From: codereader Date: Sat, 27 Jan 2024 06:39:22 +0100 Subject: [PATCH] #6467: Escape quotes and line breaks when writing entity key values. --- radiantcore/map/format/Doom3MapWriter.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/radiantcore/map/format/Doom3MapWriter.cpp b/radiantcore/map/format/Doom3MapWriter.cpp index f44a2f2d9a..468d6f3cdf 100644 --- a/radiantcore/map/format/Doom3MapWriter.cpp +++ b/radiantcore/map/format/Doom3MapWriter.cpp @@ -14,10 +14,12 @@ namespace map namespace { -// Escape the line break characters in the given input string to \n -inline std::string escapeLineBreaks(const std::string& input) +// Escape line breaks and quotes in the given input string +inline std::string escapeEntityKeyValue(const std::string& input) { - return string::replace_all_copy(input, "\n", "\\n"); + auto result = string::replace_all_copy(input, "\n", "\\n"); + string::replace_all(result, "\"", "\\\""); // replace " with \" + return result; } } @@ -55,7 +57,7 @@ void Doom3MapWriter::writeEntityKeyValues(const IEntityNodePtr& entity, std::ost // Export the entity key values entity->getEntity().forEachKeyValue([&](const std::string& key, const std::string& value) { - stream << "\"" << key << "\" \"" << escapeLineBreaks(value) << "\"" << std::endl; + stream << "\"" << escapeEntityKeyValue(key) << "\" \"" << escapeEntityKeyValue(value) << "\"" << std::endl; }); }