From 29cf8ff2fea1a8f0e1f56b9f8a07fd7ee68c1770 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Mon, 21 Jun 2010 13:31:21 +0100 Subject: Convert bad chars to underscore diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c index e90d4fb..ffab748 100644 --- a/lib/tests/test_utils.c +++ b/lib/tests/test_utils.c @@ -298,6 +298,12 @@ main (int argc, char **argv) ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" ); + np_enable_state("bad^chars$in@here", 77); + temp_state_key = temp_nagios_plugin->state; + ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); + ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" ); + ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/bad_chars_in_here"), "Got internal filename" ); + np_enable_state("funnykeyname", 54); temp_state_key = temp_nagios_plugin->state; ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); diff --git a/lib/utils_base.c b/lib/utils_base.c index e6b20c8..b86ba73 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c @@ -47,6 +47,9 @@ void np_init( char *plugin_name ) { void np_cleanup() { if (this_nagios_plugin!=NULL) { if(this_nagios_plugin->state!=NULL) { + np_free(this_nagios_plugin->state->state_data->data); + np_free(this_nagios_plugin->state->state_data); + np_free(this_nagios_plugin->state->name); np_free(this_nagios_plugin->state); } np_free(this_nagios_plugin->plugin_name); @@ -351,7 +354,7 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { * parse of argv, so that uniqueness in parameters are reflected there. */ char *_np_state_generate_key() { - return "Ahash"; + return strdup("Ahash"); } void _cleanup_state_data() { @@ -383,6 +386,8 @@ char* _np_state_calculate_location_prefix(){ void np_enable_state(char *keyname, int expected_data_version) { state_key *this_state = NULL; char *temp_filename = NULL; + char *temp_keyname = NULL; + char *p=NULL; if(this_nagios_plugin==NULL) die(STATE_UNKNOWN, _("This requires np_init to be called")); @@ -393,15 +398,25 @@ void np_enable_state(char *keyname, int expected_data_version) { die(STATE_UNKNOWN, _("Cannot allocate memory for state key")); if(keyname==NULL) { - keyname = _np_state_generate_key(); + temp_keyname = _np_state_generate_key(); + } else { + temp_keyname = strdup(keyname); + } + /* Convert all non-alphanumerics to _ */ + p = temp_keyname; + while(*p!='\0') { + if(! isalnum(*p)) { + *p='_'; + } + p++; } - this_state->name=keyname; + this_state->name=temp_keyname; this_state->plugin_name=this_nagios_plugin->plugin_name; this_state->data_version=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, keyname); + asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, this_state->name); this_state->_filename=temp_filename; this_nagios_plugin->state = this_state; -- cgit v0.10-9-g596f