Преглед на файлове

lib/utils_base.c - check asprintf returns

Check asprintf returns for proper error handing. Considering we are dying on failure, no real need for xasprintf.
Spenser Reinhardt преди 11 години
родител
ревизия
3fcec3358d
променени са 1 файла, в които са добавени 10 реда и са изтрити 3 реда
  1. 10 3
      lib/utils_base.c

+ 10 - 3
lib/utils_base.c

@@ -454,6 +454,7 @@ void np_enable_state(char *keyname, int expected_data_version) {
 	char *temp_filename = NULL;
 	char *temp_keyname = NULL;
 	char *p=NULL;
+	int ret;
 
 	if(this_nagios_plugin==NULL)
 		die(STATE_UNKNOWN, _("This requires np_init to be called"));
@@ -484,7 +485,9 @@ void np_enable_state(char *keyname, int expected_data_version) {
 	this_state->state_data=NULL;
 
 	/* Calculate filename */
-	asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, this_state->name);
+	ret = asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, this_state->name);
+	if (ret < 0)
+		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 	this_state->_filename=temp_filename;
 
 	this_nagios_plugin->state = this_state;
@@ -620,7 +623,9 @@ void np_state_write_string(time_t data_time, char *data_string) {
 	
 	/* If file doesn't currently exist, create directories */
 	if(access(this_nagios_plugin->state->_filename,F_OK)!=0) {
-		asprintf(&directories, "%s", this_nagios_plugin->state->_filename);
+		result = asprintf(&directories, "%s", this_nagios_plugin->state->_filename);
+		if (result < 0)
+			die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 		if(directories==NULL)
 			die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
 			    strerror(errno));
@@ -639,7 +644,9 @@ void np_state_write_string(time_t data_time, char *data_string) {
 		np_free(directories);
 	}
 
-	asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename);
+	result = asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename);
+	if (result < 0)
+		die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
 	if(temp_file==NULL)
 		die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
 		    strerror(errno));