summaryrefslogtreecommitdiffstats
path: root/lib/utils_base.c
diff options
context:
space:
mode:
authorAndreas Ericsson <ae@op5.se>2012-11-02 14:51:09 +0100
committerThomas Guyot-Sionnest <dermoth@aei.ca>2012-11-02 22:25:37 -0400
commit364ce21b1048d1125b8d3fd6744661ab581d08c7 (patch)
tree7450e9bd2d1ee3b0ae4c90e496e9fa28985b3763 /lib/utils_base.c
parentbd782990566eec91b8312cfc2765a7e2bd9e67da (diff)
downloadmonitoring-plugins-364ce21b1048d1125b8d3fd6744661ab581d08c7.tar.gz
lib/utils_base.c: Stop dumping core on bad args to check_snmp
Since the state patch introduction, we've been freeing uninitialized memory in lib/utils_base.c::np_cleanup(), which caused coredumps with check_snmp when illegal threshold ranges (for example) were passed, or when we called 'die' without having read any state. This patch fixes it by replacing the malloc() calls in there (all of them, since using malloc() is almost always an error) with calloc(). malloc() either doesn't initialize the memory at all, or taints it with a special marker so it can tell us when we're free()'ing memory that hasn't been initialized. calloc() explicitly initializes the allocated memory to nul bytes, which is a zero-cost operation when we get the memory from the kernel (which alread does that) and almost always desirable everywhere else. Signed-off-by: Andreas Ericsson <ae@op5.se>
Diffstat (limited to 'lib/utils_base.c')
-rw-r--r--lib/utils_base.c16
1 files changed, 8 insertions, 8 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c
index 1f705d97..c93e9c3c 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -35,7 +35,7 @@ nagios_plugin *this_nagios_plugin=NULL;
35 35
36void np_init( char *plugin_name, int argc, char **argv ) { 36void np_init( char *plugin_name, int argc, char **argv ) {
37 if (this_nagios_plugin==NULL) { 37 if (this_nagios_plugin==NULL) {
38 this_nagios_plugin = malloc(sizeof(nagios_plugin)); 38 this_nagios_plugin = calloc(1, sizeof(nagios_plugin));
39 if (this_nagios_plugin==NULL) { 39 if (this_nagios_plugin==NULL) {
40 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 40 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
41 strerror(errno)); 41 strerror(errno));
@@ -108,7 +108,7 @@ range
108 double end; 108 double end;
109 char *end_str; 109 char *end_str;
110 110
111 temp_range = (range *) malloc(sizeof(range)); 111 temp_range = (range *) calloc(1, sizeof(range));
112 112
113 /* Set defaults */ 113 /* Set defaults */
114 temp_range->start = 0; 114 temp_range->start = 0;
@@ -154,7 +154,7 @@ _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_st
154{ 154{
155 thresholds *temp_thresholds = NULL; 155 thresholds *temp_thresholds = NULL;
156 156
157 if ((temp_thresholds = malloc(sizeof(thresholds))) == NULL) 157 if ((temp_thresholds = calloc(1, sizeof(thresholds))) == NULL)
158 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 158 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
159 strerror(errno)); 159 strerror(errno));
160 160
@@ -335,13 +335,13 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
335 if (tmp = index(varlist, sep)) { 335 if (tmp = index(varlist, sep)) {
336 /* Value is delimited by a comma */ 336 /* Value is delimited by a comma */
337 if (tmp-varlist == 0) continue; 337 if (tmp-varlist == 0) continue;
338 value = (char *)malloc(tmp-varlist+1); 338 value = (char *)calloc(1, tmp-varlist+1);
339 strncpy(value, varlist, tmp-varlist); 339 strncpy(value, varlist, tmp-varlist);
340 value[tmp-varlist] = '\0'; 340 value[tmp-varlist] = '\0';
341 } else { 341 } else {
342 /* Value is delimited by a \0 */ 342 /* Value is delimited by a \0 */
343 if (strlen(varlist) == 0) continue; 343 if (strlen(varlist) == 0) continue;
344 value = (char *)malloc(strlen(varlist) + 1); 344 value = (char *)calloc(1, strlen(varlist) + 1);
345 strncpy(value, varlist, strlen(varlist)); 345 strncpy(value, varlist, strlen(varlist));
346 value[strlen(varlist)] = '\0'; 346 value[strlen(varlist)] = '\0';
347 } 347 }
@@ -431,7 +431,7 @@ void np_enable_state(char *keyname, int expected_data_version) {
431 if(this_nagios_plugin==NULL) 431 if(this_nagios_plugin==NULL)
432 die(STATE_UNKNOWN, _("This requires np_init to be called")); 432 die(STATE_UNKNOWN, _("This requires np_init to be called"));
433 433
434 this_state = (state_key *) malloc(sizeof(state_key)); 434 this_state = (state_key *) calloc(1, sizeof(state_key));
435 if(this_state==NULL) 435 if(this_state==NULL)
436 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 436 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
437 strerror(errno)); 437 strerror(errno));
@@ -482,7 +482,7 @@ state_data *np_state_read() {
482 statefile = fopen( this_nagios_plugin->state->_filename, "r" ); 482 statefile = fopen( this_nagios_plugin->state->_filename, "r" );
483 if(statefile!=NULL) { 483 if(statefile!=NULL) {
484 484
485 this_state_data = (state_data *) malloc(sizeof(state_data)); 485 this_state_data = (state_data *) calloc(1, sizeof(state_data));
486 if(this_state_data==NULL) 486 if(this_state_data==NULL)
487 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 487 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
488 strerror(errno)); 488 strerror(errno));
@@ -517,7 +517,7 @@ int _np_state_read_file(FILE *f) {
517 time(&current_time); 517 time(&current_time);
518 518
519 /* Note: This introduces a limit of 1024 bytes in the string data */ 519 /* Note: This introduces a limit of 1024 bytes in the string data */
520 line = (char *) malloc(1024); 520 line = (char *) calloc(1, 1024);
521 if(line==NULL) 521 if(line==NULL)
522 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), 522 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
523 strerror(errno)); 523 strerror(errno));