From b65a3691d602fbd71fc43b0e72dc9b4c34155cd6 Mon Sep 17 00:00:00 2001 From: Jared Casper Date: Fri, 21 Aug 2009 19:28:19 -0700 Subject: [PATCH] Fix memory leak and gcc4.4 compilation. There was a small memory leak in parm_to_defparam_list where space for "key" and "value" was being allocated via strdup and never freed. This was fixed by freeing them at each return point. In that function, key and value were defined to be const char* but were later mutated. gcc4.4 is more strict with const-ness in some functions than previous versions (see http://gcc.gnu.org/gcc-4.4/porting_to.html section "Strict null-terminated sequence utilities"), so this caused compilation to fail. Fixed by removing the const modifier in the declaration of those two variables and related nkey variable. This also necessitated malloc'ing room for value in one path through the code, which made memory management easier as well. --- pform.cc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/pform.cc b/pform.cc index 9041e70ec..b7686e81e 100644 --- a/pform.cc +++ b/pform.cc @@ -50,23 +50,24 @@ map pform_primitives; */ void parm_to_defparam_list(const string¶m) { - const char* key; - const char* value; + char* key; + char* value; unsigned off = param.find('='); if (off > param.size()) { key = strdup(param.c_str()); - value = ""; + value = (char*)malloc(1); + *value = '\0'; } else { key = strdup(param.substr(0, off).c_str()); - value = strdup(param.substr(off+1).c_str()); + value = strdup(param.substr(off+1).c_str()); } // Resolve hierarchical name for defparam. Remember // to deal with bit select for generate scopes. Bit // select expression should be constant interger. pform_name_t name; - const char *nkey = key; + char *nkey = key; char *ptr = strchr(key, '.'); while (ptr != 0) { *ptr++ = '\0'; @@ -78,6 +79,8 @@ void parm_to_defparam_list(const string¶m) char *bit_r = strchr(bit_l, ']'); if (bit_r == 0) { cerr << ": error: missing ']' for defparam: " << nkey << endl; + free(key); + free(value); return; } *bit_r = '\0'; @@ -85,6 +88,8 @@ void parm_to_defparam_list(const string¶m) while (*(bit_l+i) != '\0') if (!isdigit(*(bit_l+i++))) { cerr << ": error: scope index expression is not constant: " << nkey << endl; + free(key); + free(value); return; } name_component_t tmp(lex_strings.make(nkey)); @@ -156,6 +161,8 @@ void parm_to_defparam_list(const string¶m) val = make_unsized_octal(num); else { cerr << ": error: value specify error for defparam: " << name << endl; + free(key); + free(value); return; } @@ -185,6 +192,8 @@ void parm_to_defparam_list(const string¶m) } } } + free(key); + free(value); } /*