]> granicus.if.org Git - icinga2/commitdiff
Fix: Escaped sequences not properly generated with 'node update-config'
authorMichael Friedrich <michael.friedrich@netways.de>
Thu, 21 Jan 2016 17:14:53 +0000 (18:14 +0100)
committerMichael Friedrich <michael.friedrich@netways.de>
Thu, 21 Jan 2016 17:14:53 +0000 (18:14 +0100)
fixes #10989

lib/cli/repositoryutility.cpp
lib/cli/repositoryutility.hpp

index 9dba28caaff4c825c1a84d2d553f76a44ffea42b..eabe0bba581ad6f23fe8479c0830a8c4b8cbf993 100644 (file)
@@ -683,7 +683,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";
@@ -712,6 +712,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<Array>()) {
@@ -720,11 +733,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)
index 23d2a69267808e34f599d0ab6536e1e28f596a5b..02ea03dff5e6b046da1d994a8dd64b9963d1703d 100644 (file)
@@ -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);