diff options
author | Karl DeBisschop <kdebisschop@users.sourceforge.net> | 2003-01-03 03:24:17 +0000 |
---|---|---|
committer | Karl DeBisschop <kdebisschop@users.sourceforge.net> | 2003-01-03 03:24:17 +0000 |
commit | 86bf45146e30e16bde8b27b02ad95fac26838170 (patch) | |
tree | 9e4e4fa6451672f33303954f3b4072d6e526751c /plugins/utils.c | |
parent | 6f1fc7e3a0196dabe442ef3e4b00c285c954ccd5 (diff) | |
download | monitoring-plugins-86bf45146e30e16bde8b27b02ad95fac26838170.tar.gz |
protect against some null strings, make formats more uniform
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@234 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/utils.c')
-rw-r--r-- | plugins/utils.c | 84 |
1 files changed, 52 insertions, 32 deletions
diff --git a/plugins/utils.c b/plugins/utils.c index da9ccedd..22020d72 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -165,6 +165,9 @@ is_dotted_quad (char *address) int o1, o2, o3, o4; char c[1]; + if (!address) + return FALSE; + if (sscanf (address, "%d.%d.%d.%d%c", &o1, &o2, &o3, &o4, c) != 4) return FALSE; else if (o1 > 255 || o2 > 255 || o3 > 255 || o4 > 255) @@ -185,18 +188,18 @@ is_dotted_quad (char *address) int is_hostname (char *s1) { - if (strlen (s1) > 63) + if (!s1 || strlen (s1) > 63) { + return FALSE; + } + if (strcspn (s1, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789-.") != 0) { return FALSE; - if (strcspn - (s1, - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUWVXYZ0123456789-.") != - 0) return FALSE; - if (strspn (s1, "0123456789-.") == 1) + } + if (strspn (s1, "0123456789-.") == 1) { return FALSE; + } while ((s1 = index (s1, '.'))) { s1++; if (strspn (s1, "0123456789-.") == 1) { - printf ("%s\n", s1); return FALSE; } } @@ -208,33 +211,40 @@ is_numeric (char *number) { char tmp[1]; float x; - if (sscanf (number, "%f%c", &x, tmp) == 1) - return (TRUE); - return (FALSE); + + if (!number) + return FALSE; + else if (sscanf (number, "%f%c", &x, tmp) == 1) + return TRUE; + else + return FALSE; } int is_positive (char *number) { if (is_numeric (number) && atof (number) > 0.0) - return (TRUE); - return (FALSE); + return TRUE; + else + return FALSE; } int is_negative (char *number) { if (is_numeric (number) && atof (number) < 0.0) - return (TRUE); - return (FALSE); + return TRUE; + else + return FALSE; } int is_nonnegative (char *number) { if (is_numeric (number) && atof (number) >= 0.0) - return (TRUE); - return (FALSE); + return TRUE; + else + return FALSE; } int @@ -242,8 +252,9 @@ is_percentage (char *number) { int x; if (is_numeric (number) && (x = atof (number)) >= 0 && x <= 100) - return (TRUE); - return (FALSE); + return TRUE; + else + return FALSE; } int @@ -251,37 +262,42 @@ is_integer (char *number) { long int n; - if (strspn (number, "-0123456789 ") != strlen (number)) - return (FALSE); + if (!number || (strspn (number, "-0123456789 ") != strlen (number))) + return FALSE; n = strtol (number, NULL, 10); + if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) - return (TRUE); - return (FALSE); + return TRUE; + else + return FALSE; } int is_intpos (char *number) { if (is_integer (number) && atoi (number) > 0) - return (TRUE); - return (FALSE); + return TRUE; + else + return FALSE; } int is_intneg (char *number) { if (is_integer (number) && atoi (number) < 0) - return (TRUE); - return (FALSE); + return TRUE; + else + return FALSE; } int is_intnonneg (char *number) { if (is_integer (number) && atoi (number) >= 0) - return (TRUE); - return (FALSE); + return TRUE; + else + return FALSE; } int @@ -289,16 +305,20 @@ is_intpercent (char *number) { int i; if (is_integer (number) && (i = atoi (number)) >= 0 && i <= 100) - return (TRUE); - return (FALSE); + return TRUE; + else + return FALSE; } int is_option (char *str) { - if (strspn (str, "-") == 1 || strspn (str, "-") == 2) + if (!str) + return FALSE; + else if (strspn (str, "-") == 1 || strspn (str, "-") == 2) return TRUE; - return FALSE; + else + return FALSE; } |