summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTon Voon <ton.voon@opsera.com>2010-06-21 15:24:41 (GMT)
committerTon Voon <ton.voon@opsera.com>2010-06-21 15:24:41 (GMT)
commitbc9c95bdf4b47ab5d39dbdf35165837a4c4140c1 (patch)
tree455de67a1a30430cb28a784ea5958019f4951a94
parent833fd7ca04ae1e746db1feafae3bae1bbf4a10c3 (diff)
downloadmonitoring-plugins-bc9c95bdf4b47ab5d39dbdf35165837a4c4140c1.tar.gz
Check return codes from malloc and strdup
-rw-r--r--lib/utils_base.c34
1 files changed, 25 insertions, 9 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c
index cfff7cb..1234e25 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -37,10 +37,12 @@ void 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 = malloc(sizeof(nagios_plugin));
39 if (this_nagios_plugin==NULL) { 39 if (this_nagios_plugin==NULL) {
40 die(STATE_UNKNOWN, _("Cannot allocate memory: %s\n"), 40 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
41 strerror(errno)); 41 strerror(errno));
42 } 42 }
43 this_nagios_plugin->plugin_name = strdup(plugin_name); 43 this_nagios_plugin->plugin_name = strdup(plugin_name);
44 if (this_nagios_plugin->plugin_name==NULL)
45 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
44 this_nagios_plugin->argc = argc; 46 this_nagios_plugin->argc = argc;
45 this_nagios_plugin->argv = argv; 47 this_nagios_plugin->argv = argv;
46 } 48 }
@@ -142,7 +144,7 @@ _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_st
142 thresholds *temp_thresholds = NULL; 144 thresholds *temp_thresholds = NULL;
143 145
144 if ((temp_thresholds = malloc(sizeof(thresholds))) == NULL) 146 if ((temp_thresholds = malloc(sizeof(thresholds))) == NULL)
145 die(STATE_UNKNOWN, _("Cannot allocate memory: %s\n"), 147 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
146 strerror(errno)); 148 strerror(errno));
147 149
148 temp_thresholds->warning = NULL; 150 temp_thresholds->warning = NULL;
@@ -361,6 +363,7 @@ char *_np_state_generate_key() {
361 char **argv = this_nagios_plugin->argv; 363 char **argv = this_nagios_plugin->argv;
362 unsigned char result[20]; 364 unsigned char result[20];
363 char keyname[41]; 365 char keyname[41];
366 char *p=NULL;
364 367
365 sha1_init_ctx(&ctx); 368 sha1_init_ctx(&ctx);
366 369
@@ -375,7 +378,11 @@ char *_np_state_generate_key() {
375 } 378 }
376 keyname[40]='\0'; 379 keyname[40]='\0';
377 380
378 return strdup(keyname); 381 p = strdup(keyname);
382 if(p==NULL) {
383 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
384 }
385 return p;
379} 386}
380 387
381void _cleanup_state_data() { 388void _cleanup_state_data() {
@@ -414,14 +421,16 @@ void np_enable_state(char *keyname, int expected_data_version) {
414 die(STATE_UNKNOWN, _("This requires np_init to be called")); 421 die(STATE_UNKNOWN, _("This requires np_init to be called"));
415 422
416 this_state = (state_key *) malloc(sizeof(state_key)); 423 this_state = (state_key *) malloc(sizeof(state_key));
417
418 if(this_state==NULL) 424 if(this_state==NULL)
419 die(STATE_UNKNOWN, _("Cannot allocate memory for state key")); 425 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
426 strerror(errno));
420 427
421 if(keyname==NULL) { 428 if(keyname==NULL) {
422 temp_keyname = _np_state_generate_key(); 429 temp_keyname = _np_state_generate_key();
423 } else { 430 } else {
424 temp_keyname = strdup(keyname); 431 temp_keyname = strdup(keyname);
432 if(temp_keyname==NULL)
433 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
425 } 434 }
426 /* Convert all non-alphanumerics to _ */ 435 /* Convert all non-alphanumerics to _ */
427 p = temp_keyname; 436 p = temp_keyname;
@@ -465,9 +474,9 @@ state_data *np_state_read() {
465 if(statefile!=NULL) { 474 if(statefile!=NULL) {
466 475
467 this_state_data = (state_data *) malloc(sizeof(state_data)); 476 this_state_data = (state_data *) malloc(sizeof(state_data));
468
469 if(this_state_data==NULL) 477 if(this_state_data==NULL)
470 die(STATE_UNKNOWN, _("Cannot allocate memory for state data")); 478 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
479 strerror(errno));
471 480
472 this_state_data->data=NULL; 481 this_state_data->data=NULL;
473 this_nagios_plugin->state->state_data = this_state_data; 482 this_nagios_plugin->state->state_data = this_state_data;
@@ -500,6 +509,9 @@ int _np_state_read_file(FILE *f) {
500 509
501 /* Note: This introduces a limit of 1024 bytes in the string data */ 510 /* Note: This introduces a limit of 1024 bytes in the string data */
502 line = (char *) malloc(1024); 511 line = (char *) malloc(1024);
512 if(line==NULL)
513 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
514 strerror(errno));
503 515
504 while(!failure && (fgets(line,1024,f))!=NULL){ 516 while(!failure && (fgets(line,1024,f))!=NULL){
505 pos=strlen(line); 517 pos=strlen(line);
@@ -536,6 +548,8 @@ int _np_state_read_file(FILE *f) {
536 break; 548 break;
537 case STATE_DATA_TEXT: 549 case STATE_DATA_TEXT:
538 this_nagios_plugin->state->state_data->data = strdup(line); 550 this_nagios_plugin->state->state_data->data = strdup(line);
551 if(this_nagios_plugin->state->state_data->data==NULL)
552 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
539 expected=STATE_DATA_END; 553 expected=STATE_DATA_END;
540 status=TRUE; 554 status=TRUE;
541 break; 555 break;
@@ -573,7 +587,8 @@ void np_state_write_string(time_t data_time, char *data_string) {
573 if(access(this_nagios_plugin->state->_filename,F_OK)!=0) { 587 if(access(this_nagios_plugin->state->_filename,F_OK)!=0) {
574 asprintf(&directories, "%s", this_nagios_plugin->state->_filename); 588 asprintf(&directories, "%s", this_nagios_plugin->state->_filename);
575 if(directories==NULL) 589 if(directories==NULL)
576 die(STATE_UNKNOWN, _("Cannot malloc")); 590 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
591 strerror(errno));
577 592
578 for(p=directories+1; *p; p++) { 593 for(p=directories+1; *p; p++) {
579 if(*p=='/') { 594 if(*p=='/') {
@@ -591,7 +606,8 @@ void np_state_write_string(time_t data_time, char *data_string) {
591 606
592 asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename); 607 asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename);
593 if(temp_file==NULL) 608 if(temp_file==NULL)
594 die(STATE_UNKNOWN, _("Cannot malloc temporary state file")); 609 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
610 strerror(errno));
595 611
596 if((fd=mkstemp(temp_file))==-1) { 612 if((fd=mkstemp(temp_file))==-1) {
597 np_free(temp_file); 613 np_free(temp_file);