[icinga-checkins] icinga.org: icinga2/master: compat: Escape new-lines in commands.

git at icinga.org git at icinga.org
Tue Jul 2 09:18:08 CEST 2013


Module: icinga2
Branch: master
Commit: 049fbcb3cbfae7f568ea2c176edc44be8ae5e793
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=049fbcb3cbfae7f568ea2c176edc44be8ae5e793

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Tue Jul  2 09:16:06 2013 +0200

compat: Escape new-lines in commands.

---

 components/compat/compatcomponent.cpp |   13 ++++++++++---
 components/compat/compatcomponent.h   |    2 ++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp
index ba108d5..6cf8bb0 100644
--- a/components/compat/compatcomponent.cpp
+++ b/components/compat/compatcomponent.cpp
@@ -266,10 +266,10 @@ void CompatComponent::DumpCommand(std::ostream& fp, const Command::Ptr& command)
 		String arg;
 		BOOST_FOREACH(arg, args) {
 			// This is obviously incorrect for non-trivial cases.
-			fp << " \"" << arg << "\"";
+			fp << " \"" << EscapeString(arg) << "\"";
 		}
 	} else if (!commandLine.IsEmpty()) {
-		fp << Convert::ToString(commandLine);
+		fp << EscapeString(Convert::ToString(commandLine));
 	} else {
 		fp << "<internal>";
 	}
@@ -391,6 +391,13 @@ void CompatComponent::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
 	   << "\n";
 }
 
+String CompatComponent::EscapeString(const String& str)
+{
+	String result = str;
+	boost::algorithm::replace_all(result, "\n", "\\n");
+	return result;
+}
+
 void CompatComponent::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
 {
 	ASSERT(service->OwnsLock());
@@ -428,7 +435,7 @@ void CompatComponent::DumpServiceStatusAttrs(std::ostream& fp, const Service::Pt
 
 		if (line_end > 0 && line_end != String::NPos) {
 			long_output = raw_output.SubStr(line_end+1, raw_output.GetLength());
-			boost::algorithm::replace_all(long_output, "\n", "\\n");
+			long_output = EscapeString(long_output);
 		}
 
 		boost::algorithm::replace_all(output, "\n", "\\n");
diff --git a/components/compat/compatcomponent.h b/components/compat/compatcomponent.h
index 39afaa4..e36ec07 100644
--- a/components/compat/compatcomponent.h
+++ b/components/compat/compatcomponent.h
@@ -112,6 +112,8 @@ private:
 	void DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object);
 
 	void StatusTimerHandler(void);
+
+	static String EscapeString(const String& str);
 };
 
 }





More information about the icinga-checkins mailing list