From 3a5b8ef64c074e33670de570cc19f26152f1d59f Mon Sep 17 00:00:00 2001 From: VilhoRaatikka Date: Thu, 21 Aug 2014 23:08:21 +0300 Subject: [PATCH] query_classifier.cc: cleaned up and simplified skygw_get_canonical skygw_util.cc:fixed memory allocation issue where terminating byte wasn't counted. Added some error checks. --- query_classifier/query_classifier.cc | 20 ++--------------- utils/skygw_utils.cc | 32 ++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/query_classifier/query_classifier.cc b/query_classifier/query_classifier.cc index 9c2ab146a..2f29ddba1 100644 --- a/query_classifier/query_classifier.cc +++ b/query_classifier/query_classifier.cc @@ -860,7 +860,6 @@ char* skygw_get_canonical( THD* thd; LEX* lex; bool found = false; - char* newstr = NULL; Item* item; char* querystr; @@ -898,26 +897,11 @@ char* skygw_get_canonical( itype == Item::VARBIN_ITEM || itype == Item::NULL_ITEM)) { - if (!found) - { - newstr = replace_literal(querystr, item->name, "?"); - if (newstr != NULL) - { - free(querystr); - found = true; - } - } - else - { - char* prevstr = newstr; - - newstr = replace_literal(prevstr, item->name, "?"); - free(prevstr); - } + querystr = replace_literal(querystr, item->name, "?"); } } /*< for */ retblock: - return newstr; + return querystr; } diff --git a/utils/skygw_utils.cc b/utils/skygw_utils.cc index c9c3fb77f..6ffafd7c3 100644 --- a/utils/skygw_utils.cc +++ b/utils/skygw_utils.cc @@ -1882,7 +1882,7 @@ char* replace_literal( const char* needle, const char* replacement) { - const char* prefix = "[ =',\\(]"; /*< ' ','=','(',''',',' are allowed before needle */ + const char* prefix = "[ ='\",\\(]"; /*< ' ','=','(',''',''"',',' are allowed before needle */ const char* suffix = "[$^[:alnum:]]?"; /*< alpha-num chars aren't allowed after the needle */ char* search_re; char* newstr; @@ -1894,9 +1894,27 @@ char* replace_literal( size_t hlen = strlen(haystack); search_re = (char *)malloc(strlen(prefix)+nlen+strlen(suffix)+1); + + if (search_re == NULL) + { + fprintf(stderr, "Regex memory allocation failed : %s\n", + strerror(errno)); + newstr = haystack; + goto retblock; + } + sprintf(search_re, "%s%s%s", prefix, needle, suffix); - /** Allocate memory for new string */ - newstr = (char *)malloc(hlen-nlen+rlen); + /** Allocate memory for new string +1 for terminating byte */ + newstr = (char *)malloc(hlen-nlen+rlen+1); + + if (newstr == NULL) + { + fprintf(stderr, "Regex memory allocation failed : %s\n", + strerror(errno)); + free(search_re); + newstr = haystack; + goto retblock; + } rc = regcomp(&re, search_re, REG_EXTENDED); ss_dassert(rc == 0); @@ -1910,7 +1928,7 @@ char* replace_literal( search_re, error_message); free(search_re); - newstr = NULL; + newstr = haystack; goto retblock; } rc = regexec(&re, haystack, 1, &match, 0); @@ -1918,7 +1936,7 @@ char* replace_literal( if (rc != 0) { free(search_re); - newstr = NULL; + newstr = haystack; goto retblock; } memcpy(newstr, haystack, match.rm_so+1); @@ -1927,8 +1945,8 @@ char* replace_literal( memcpy(newstr+match.rm_so+1+rlen, haystack+match.rm_so+1+nlen, hlen-(match.rm_so+1)-nlen+1); regfree(&re); - -retblock: + free(haystack); +retblock: return newstr; }