From abce99daffa0990ff538a9a64fe929bd10399efd Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Thu, 21 Jan 2016 18:14:53 +0100 Subject: [PATCH] Fix: Escaped sequences not properly generated with 'node update-config' fixes #10989 --- lib/cli/repositoryutility.cpp | 19 ++++++++++++++++--- lib/cli/repositoryutility.hpp | 1 + 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/cli/repositoryutility.cpp b/lib/cli/repositoryutility.cpp index 6fbd6252b..89f1e4bcf 100644 --- a/lib/cli/repositoryutility.cpp +++ b/lib/cli/repositoryutility.cpp @@ -680,7 +680,7 @@ void RepositoryUtility::FormatChangelogEntry(std::ostream& fp, const Dictionary: */ void RepositoryUtility::SerializeObject(std::ostream& fp, const String& name, const String& type, const Dictionary::Ptr& object) { - fp << "object " << type << " \"" << name << "\" {\n"; + fp << "object " << type << " \"" << EscapeIcingaString(name) << "\" {\n"; if (!object) { fp << "}\n"; @@ -709,6 +709,19 @@ void RepositoryUtility::SerializeObject(std::ostream& fp, const String& name, co fp << "}\n"; } +String RepositoryUtility::EscapeIcingaString(const String& str) +{ + String result = str; + boost::algorithm::replace_all(result, "\\", "\\\\"); + boost::algorithm::replace_all(result, "\n", "\\n"); + boost::algorithm::replace_all(result, "\t", "\\t"); + boost::algorithm::replace_all(result, "\r", "\\r"); + boost::algorithm::replace_all(result, "\b", "\\b"); + boost::algorithm::replace_all(result, "\f", "\\f"); + boost::algorithm::replace_all(result, "\"", "\\\""); + return result; +} + void RepositoryUtility::FormatValue(std::ostream& fp, const Value& val) { if (val.IsObjectType()) { @@ -717,11 +730,11 @@ void RepositoryUtility::FormatValue(std::ostream& fp, const Value& val) } if (val.IsString()) { - fp << "\"" << Convert::ToString(val) << "\""; + fp << "\"" << EscapeIcingaString(val) << "\""; return; } - fp << Convert::ToString(val); + fp << EscapeIcingaString(val); } void RepositoryUtility::FormatArray(std::ostream& fp, const Array::Ptr& arr) diff --git a/lib/cli/repositoryutility.hpp b/lib/cli/repositoryutility.hpp index 23d2a6926..02ea03dff 100644 --- a/lib/cli/repositoryutility.hpp +++ b/lib/cli/repositoryutility.hpp @@ -96,6 +96,7 @@ private: /* config print helpers */ static void SerializeObject(std::ostream& fp, const String& name, const String& type, const Dictionary::Ptr& object); + static String EscapeIcingaString(const String& str); static void FormatValue(std::ostream& fp, const Value& val); static void FormatArray(std::ostream& fp, const Array::Ptr& arr);