[icinga-checkins] icinga.org: icinga2/next: Error Messages: Catch boost::regex exceptions.

git at icinga.org git at icinga.org
Thu Jun 5 18:29:54 CEST 2014


Module: icinga2
Branch: next
Commit: 6a080edf80631e2df9a1335af199f2fb3df7c0e1
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=6a080edf80631e2df9a1335af199f2fb3df7c0e1

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Thu Jun  5 16:13:28 2014 +0200

Error Messages: Catch boost::regex exceptions.

Refs #6070

---

 components/livestatus/attributefilter.cpp |   28 ++++++++++++++++++++--------
 lib/base/scriptutils.cpp                  |   13 ++++++++++---
 2 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/components/livestatus/attributefilter.cpp b/components/livestatus/attributefilter.cpp
index 72aa8e2..bf693bc 100644
--- a/components/livestatus/attributefilter.cpp
+++ b/components/livestatus/attributefilter.cpp
@@ -60,10 +60,16 @@ bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
 			else
 				return (static_cast<String>(value) == m_Operand);
 		} else if (m_Operator == "~") {
-			boost::regex expr(m_Operand.GetData());
-			String operand = value;
-			boost::smatch what;
-			bool ret = boost::regex_search(operand.GetData(), what, expr);
+			bool ret;
+			try {
+				boost::regex expr(m_Operand.GetData());
+				String operand = value;
+				boost::smatch what;
+				ret = boost::regex_search(operand.GetData(), what, expr);
+			} catch (boost::exception&) {
+				Log(LogWarning, "AttributeFilter", "Regex '" + m_Operand + " " + m_Operator + " " + Convert::ToString(value) + "' error.");
+				ret = false;
+			}
 
 			//Log(LogDebug, "LivestatusListener/AttributeFilter", "Attribute filter '" + m_Operand + " " + m_Operator + " " +
 			//    static_cast<String>(value) + "' " + (ret ? "matches" : "doesn't match") + "." );
@@ -72,10 +78,16 @@ bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
 		} else if (m_Operator == "=~") {
 			return string_iless()(value, m_Operand);
 		} else if (m_Operator == "~~") {
-			boost::regex expr(m_Operand.GetData(), boost::regex::icase);
-			String operand = value;
-			boost::smatch what;
-			bool ret = boost::regex_search(operand.GetData(), what, expr);
+			bool ret;
+			try {
+				boost::regex expr(m_Operand.GetData(), boost::regex::icase);
+				String operand = value;
+				boost::smatch what;
+				ret = boost::regex_search(operand.GetData(), what, expr);
+			} catch (boost::exception&) {
+				Log(LogWarning, "AttributeFilter", "Regex '" + m_Operand + " " + m_Operator + " " + Convert::ToString(value) + "' error.");
+				ret = false;
+			}
 
 			//Log(LogDebug, "LivestatusListener/AttributeFilter", "Attribute filter '" + m_Operand + " " + m_Operator + " " +
 			//    static_cast<String>(value) + "' " + (ret ? "matches" : "doesn't match") + "." );
diff --git a/lib/base/scriptutils.cpp b/lib/base/scriptutils.cpp
index c925a08..8aacbc0 100644
--- a/lib/base/scriptutils.cpp
+++ b/lib/base/scriptutils.cpp
@@ -43,9 +43,16 @@ REGISTER_SCRIPTFUNCTION(exit, &ScriptUtils::Exit);
 
 bool ScriptUtils::Regex(const String& pattern, const String& text)
 {
-	boost::regex expr(pattern.GetData());
-	boost::smatch what;
-	return boost::regex_search(text.GetData(), what, expr);
+	bool res = false;
+	try {
+		boost::regex expr(pattern.GetData());
+		boost::smatch what;
+		res = boost::regex_search(text.GetData(), what, expr);
+	} catch (boost::exception&) {
+		res = false; /* exception means something went terribly wrong */
+	}
+
+	return res;
 }
 
 int ScriptUtils::Len(const Value& value)



More information about the icinga-checkins mailing list