diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/tests/test_utils.c | 14 | ||||
| -rw-r--r-- | lib/utils_base.c | 17 |
2 files changed, 22 insertions, 9 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c index d4a08005..79e1110a 100644 --- a/lib/tests/test_utils.c +++ b/lib/tests/test_utils.c | |||
| @@ -38,7 +38,7 @@ main (int argc, char **argv) | |||
| 38 | state_data *temp_state_data; | 38 | state_data *temp_state_data; |
| 39 | time_t current_time; | 39 | time_t current_time; |
| 40 | 40 | ||
| 41 | plan_tests(141); | 41 | plan_tests(150); |
| 42 | 42 | ||
| 43 | ok( this_nagios_plugin==NULL, "nagios_plugin not initialised"); | 43 | ok( this_nagios_plugin==NULL, "nagios_plugin not initialised"); |
| 44 | 44 | ||
| @@ -132,6 +132,18 @@ main (int argc, char **argv) | |||
| 132 | ok( check_range(0, range) == TRUE, "0 - alert"); | 132 | ok( check_range(0, range) == TRUE, "0 - alert"); |
| 133 | free(range); | 133 | free(range); |
| 134 | 134 | ||
| 135 | range = parse_range_string("@1:1"); | ||
| 136 | ok( range != NULL, "'@1:1' is a valid range"); | ||
| 137 | ok( range->start == 1, "Start correct"); | ||
| 138 | ok( range->start_infinity == FALSE, "Not using negative infinity"); | ||
| 139 | ok( range->end == 1, "End correct"); | ||
| 140 | ok( range->end_infinity == FALSE, "Not using infinity"); | ||
| 141 | ok( range->alert_on == INSIDE, "Will alert on inside of this range" ); | ||
| 142 | ok( check_range(0.5, range) == FALSE, "0.5 - no alert"); | ||
| 143 | ok( check_range(1, range) == TRUE, "1 - alert"); | ||
| 144 | ok( check_range(5.2, range) == FALSE, "5.2 - no alert"); | ||
| 145 | free(range); | ||
| 146 | |||
| 135 | range = parse_range_string("1:1"); | 147 | range = parse_range_string("1:1"); |
| 136 | ok( range != NULL, "'1:1' is a valid range"); | 148 | ok( range != NULL, "'1:1' is a valid range"); |
| 137 | ok( range->start == 1, "Start correct"); | 149 | ok( range->start == 1, "Start correct"); |
diff --git a/lib/utils_base.c b/lib/utils_base.c index 1f705d97..8685e6fe 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
| @@ -28,6 +28,7 @@ | |||
| 28 | #include <stdarg.h> | 28 | #include <stdarg.h> |
| 29 | #include "utils_base.h" | 29 | #include "utils_base.h" |
| 30 | #include <fcntl.h> | 30 | #include <fcntl.h> |
| 31 | #include <sys/stat.h> | ||
| 31 | 32 | ||
| 32 | #define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } } | 33 | #define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } } |
| 33 | 34 | ||
| @@ -35,7 +36,7 @@ nagios_plugin *this_nagios_plugin=NULL; | |||
| 35 | 36 | ||
| 36 | void np_init( char *plugin_name, int argc, char **argv ) { | 37 | void np_init( char *plugin_name, int argc, char **argv ) { |
| 37 | if (this_nagios_plugin==NULL) { | 38 | if (this_nagios_plugin==NULL) { |
| 38 | this_nagios_plugin = malloc(sizeof(nagios_plugin)); | 39 | this_nagios_plugin = calloc(1, sizeof(nagios_plugin)); |
| 39 | if (this_nagios_plugin==NULL) { | 40 | if (this_nagios_plugin==NULL) { |
| 40 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), | 41 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), |
| 41 | strerror(errno)); | 42 | strerror(errno)); |
| @@ -108,7 +109,7 @@ range | |||
| 108 | double end; | 109 | double end; |
| 109 | char *end_str; | 110 | char *end_str; |
| 110 | 111 | ||
| 111 | temp_range = (range *) malloc(sizeof(range)); | 112 | temp_range = (range *) calloc(1, sizeof(range)); |
| 112 | 113 | ||
| 113 | /* Set defaults */ | 114 | /* Set defaults */ |
| 114 | temp_range->start = 0; | 115 | temp_range->start = 0; |
| @@ -154,7 +155,7 @@ _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_st | |||
| 154 | { | 155 | { |
| 155 | thresholds *temp_thresholds = NULL; | 156 | thresholds *temp_thresholds = NULL; |
| 156 | 157 | ||
| 157 | if ((temp_thresholds = malloc(sizeof(thresholds))) == NULL) | 158 | if ((temp_thresholds = calloc(1, sizeof(thresholds))) == NULL) |
| 158 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), | 159 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), |
| 159 | strerror(errno)); | 160 | strerror(errno)); |
| 160 | 161 | ||
| @@ -335,13 +336,13 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { | |||
| 335 | if (tmp = index(varlist, sep)) { | 336 | if (tmp = index(varlist, sep)) { |
| 336 | /* Value is delimited by a comma */ | 337 | /* Value is delimited by a comma */ |
| 337 | if (tmp-varlist == 0) continue; | 338 | if (tmp-varlist == 0) continue; |
| 338 | value = (char *)malloc(tmp-varlist+1); | 339 | value = (char *)calloc(1, tmp-varlist+1); |
| 339 | strncpy(value, varlist, tmp-varlist); | 340 | strncpy(value, varlist, tmp-varlist); |
| 340 | value[tmp-varlist] = '\0'; | 341 | value[tmp-varlist] = '\0'; |
| 341 | } else { | 342 | } else { |
| 342 | /* Value is delimited by a \0 */ | 343 | /* Value is delimited by a \0 */ |
| 343 | if (strlen(varlist) == 0) continue; | 344 | if (strlen(varlist) == 0) continue; |
| 344 | value = (char *)malloc(strlen(varlist) + 1); | 345 | value = (char *)calloc(1, strlen(varlist) + 1); |
| 345 | strncpy(value, varlist, strlen(varlist)); | 346 | strncpy(value, varlist, strlen(varlist)); |
| 346 | value[strlen(varlist)] = '\0'; | 347 | value[strlen(varlist)] = '\0'; |
| 347 | } | 348 | } |
| @@ -431,7 +432,7 @@ void np_enable_state(char *keyname, int expected_data_version) { | |||
| 431 | if(this_nagios_plugin==NULL) | 432 | if(this_nagios_plugin==NULL) |
| 432 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | 433 | die(STATE_UNKNOWN, _("This requires np_init to be called")); |
| 433 | 434 | ||
| 434 | this_state = (state_key *) malloc(sizeof(state_key)); | 435 | this_state = (state_key *) calloc(1, sizeof(state_key)); |
| 435 | if(this_state==NULL) | 436 | if(this_state==NULL) |
| 436 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), | 437 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), |
| 437 | strerror(errno)); | 438 | strerror(errno)); |
| @@ -482,7 +483,7 @@ state_data *np_state_read() { | |||
| 482 | statefile = fopen( this_nagios_plugin->state->_filename, "r" ); | 483 | statefile = fopen( this_nagios_plugin->state->_filename, "r" ); |
| 483 | if(statefile!=NULL) { | 484 | if(statefile!=NULL) { |
| 484 | 485 | ||
| 485 | this_state_data = (state_data *) malloc(sizeof(state_data)); | 486 | this_state_data = (state_data *) calloc(1, sizeof(state_data)); |
| 486 | if(this_state_data==NULL) | 487 | if(this_state_data==NULL) |
| 487 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), | 488 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), |
| 488 | strerror(errno)); | 489 | strerror(errno)); |
| @@ -517,7 +518,7 @@ int _np_state_read_file(FILE *f) { | |||
| 517 | time(¤t_time); | 518 | time(¤t_time); |
| 518 | 519 | ||
| 519 | /* Note: This introduces a limit of 1024 bytes in the string data */ | 520 | /* Note: This introduces a limit of 1024 bytes in the string data */ |
| 520 | line = (char *) malloc(1024); | 521 | line = (char *) calloc(1, 1024); |
| 521 | if(line==NULL) | 522 | if(line==NULL) |
| 522 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), | 523 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), |
| 523 | strerror(errno)); | 524 | strerror(errno)); |
