From 15d14d28bb8297342fd7716162a2c8c50705b69d Mon Sep 17 00:00:00 2001 From: Ricardo Maraschini Date: Wed, 22 Jan 2014 09:03:51 -0200 Subject: lib/utils_base.c: if asprintf fails, string is undefined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit if asprintf fails, string content becomes invalid. we need to check if it ran OK by checking the returned value. in case of fail, asprintf returns -1, otherwise the number of writen bytes is returned. also, on ubuntu 13.10 i've receiving a lot of warnings: "warning: ignoring return value of ‘asprintf’" this patches fixes some of them Signed-off-by: Ricardo Maraschini --- Closes #1227 --- lib/utils_base.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/utils_base.c b/lib/utils_base.c index addf26bd..3822bcf1 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -446,6 +446,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_monitoring_plugin==NULL) die(STATE_UNKNOWN, _("This requires np_init to be called")); @@ -476,9 +477,13 @@ void np_enable_state(char *keyname, int expected_data_version) { this_state->state_data=NULL; /* Calculate filename */ - asprintf(&temp_filename, "%s/%lu/%s/%s", + ret = asprintf(&temp_filename, "%s/%lu/%s/%s", _np_state_calculate_location_prefix(), (unsigned long)geteuid(), this_monitoring_plugin->plugin_name, this_state->name); + if (ret < 0) + die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), + strerror(errno)); + this_state->_filename=temp_filename; this_monitoring_plugin->state = this_state; @@ -614,8 +619,8 @@ void np_state_write_string(time_t data_time, char *data_string) { /* If file doesn't currently exist, create directories */ if(access(this_monitoring_plugin->state->_filename,F_OK)!=0) { - asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); - if(directories==NULL) + result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); + if(result < 0) die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); @@ -633,8 +638,8 @@ void np_state_write_string(time_t data_time, char *data_string) { np_free(directories); } - asprintf(&temp_file,"%s.XXXXXX",this_monitoring_plugin->state->_filename); - if(temp_file==NULL) + result = asprintf(&temp_file,"%s.XXXXXX",this_monitoring_plugin->state->_filename); + if(result < 0) die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); -- cgit v1.2.3-74-g34f1 From a143739ed286cf9ae003792dbb1ce26ce43f3312 Mon Sep 17 00:00:00 2001 From: Jan Wagner Date: Wed, 30 Jul 2014 14:53:07 +0200 Subject: Make use of xasprintf instead of asprintf --- Closes #1278 --- lib/utils_base.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/utils_base.c b/lib/utils_base.c index 3822bcf1..4fb6375d 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -477,7 +477,7 @@ void np_enable_state(char *keyname, int expected_data_version) { this_state->state_data=NULL; /* Calculate filename */ - ret = asprintf(&temp_filename, "%s/%lu/%s/%s", + ret = xasprintf(&temp_filename, "%s/%lu/%s/%s", _np_state_calculate_location_prefix(), (unsigned long)geteuid(), this_monitoring_plugin->plugin_name, this_state->name); if (ret < 0) @@ -619,7 +619,7 @@ void np_state_write_string(time_t data_time, char *data_string) { /* If file doesn't currently exist, create directories */ if(access(this_monitoring_plugin->state->_filename,F_OK)!=0) { - result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); + result = xasprintf(&directories, "%s", this_monitoring_plugin->state->_filename); if(result < 0) die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); @@ -638,7 +638,7 @@ void np_state_write_string(time_t data_time, char *data_string) { np_free(directories); } - result = asprintf(&temp_file,"%s.XXXXXX",this_monitoring_plugin->state->_filename); + result = xasprintf(&temp_file,"%s.XXXXXX",this_monitoring_plugin->state->_filename); if(result < 0) die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); -- cgit v1.2.3-74-g34f1