[nagiosplug] check_snmp: Fix support for inverse thresholds

"Nagios Plugin Development" <[email protected]> Wed, 21 Aug 2013 13:11:17 +0000
Newsgroups gmane.network.nagios.plugins.cvs
Message-ID <[email protected]>
 Module: nagiosplug
 Branch: master
 Commit: 461cea42615936f0fdf9c75fa72d0fc0a88eaff6
 Author: Holger Weiss <[email protected]>
   Date: Wed Aug 21 15:00:27 2013 +0200
    URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=461cea4

check_snmp: Fix support for inverse thresholds

Commit bd782990566eec91b8312cfc2765a7e2bd9e67da reintroduced support for
inverse threshold ranges such as "2:1", but it broke standard thresholds
such as "1:2" (by converting this range into "1") or "1:" (by converting
this range into "@:2").  This commit fixes those two bugs, plus an
off-by-one error while computing the number of bytes to allocate when
handling inverse thresholds (two additional bytes were allocated where
three were required to hold '@' and ':' and '\0').  While at it, we also
check whether malloc(3) succeeded.

---

 plugins/check_snmp.c |   12 +++++++-----
 1 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/plugins/check_snmp.c b/plugins/check_snmp.c
index 7c3bc4b..d2f2f8b 100644
--- a/plugins/check_snmp.c
+++ b/plugins/check_snmp.c
@@ -150,16 +150,18 @@ static char *fix_snmp_range(char *th)
 {
 	double left, right;
 	char *colon, *ret;
-	if (!(colon = strchr(th, ':')))
+
+	if ((colon = strchr(th, ':')) == NULL || *(colon + 1) == '\0')
 		return th;
-	*colon = 0;
 
 	left = strtod(th, NULL);
 	right = strtod(colon + 1, NULL);
-	if (right >= left) {
+	if (right >= left)
 		return th;
-	}
-	ret = malloc(strlen(th) + strlen(colon + 1) + 2);
+
+	if ((ret = malloc(strlen(th) + 2)) == NULL)
+		die(STATE_UNKNOWN, _("Cannot malloc"));
+	*colon = '\0';
 	sprintf(ret, "@%s:%s", colon + 1, th);
 	free(th);
 	return ret;


------------------------------------------------------------------------------
Introducing Performance Central, a new site from SourceForge and 
AppDynamics. Performance Central is your source for news, insights, 
analysis and resources for efficient Application Performance Management. 
Visit us today!
http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk