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.
This commit is contained in:
parent
7c6176357f
commit
b65a3691d6
19
pform.cc
19
pform.cc
|
|
@ -50,23 +50,24 @@ map<perm_string,PUdp*> pform_primitives;
|
||||||
*/
|
*/
|
||||||
void parm_to_defparam_list(const string¶m)
|
void parm_to_defparam_list(const string¶m)
|
||||||
{
|
{
|
||||||
const char* key;
|
char* key;
|
||||||
const char* value;
|
char* value;
|
||||||
unsigned off = param.find('=');
|
unsigned off = param.find('=');
|
||||||
if (off > param.size()) {
|
if (off > param.size()) {
|
||||||
key = strdup(param.c_str());
|
key = strdup(param.c_str());
|
||||||
value = "";
|
value = (char*)malloc(1);
|
||||||
|
*value = '\0';
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
key = strdup(param.substr(0, off).c_str());
|
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
|
// Resolve hierarchical name for defparam. Remember
|
||||||
// to deal with bit select for generate scopes. Bit
|
// to deal with bit select for generate scopes. Bit
|
||||||
// select expression should be constant interger.
|
// select expression should be constant interger.
|
||||||
pform_name_t name;
|
pform_name_t name;
|
||||||
const char *nkey = key;
|
char *nkey = key;
|
||||||
char *ptr = strchr(key, '.');
|
char *ptr = strchr(key, '.');
|
||||||
while (ptr != 0) {
|
while (ptr != 0) {
|
||||||
*ptr++ = '\0';
|
*ptr++ = '\0';
|
||||||
|
|
@ -78,6 +79,8 @@ void parm_to_defparam_list(const string¶m)
|
||||||
char *bit_r = strchr(bit_l, ']');
|
char *bit_r = strchr(bit_l, ']');
|
||||||
if (bit_r == 0) {
|
if (bit_r == 0) {
|
||||||
cerr << "<command line>: error: missing ']' for defparam: " << nkey << endl;
|
cerr << "<command line>: error: missing ']' for defparam: " << nkey << endl;
|
||||||
|
free(key);
|
||||||
|
free(value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
*bit_r = '\0';
|
*bit_r = '\0';
|
||||||
|
|
@ -85,6 +88,8 @@ void parm_to_defparam_list(const string¶m)
|
||||||
while (*(bit_l+i) != '\0')
|
while (*(bit_l+i) != '\0')
|
||||||
if (!isdigit(*(bit_l+i++))) {
|
if (!isdigit(*(bit_l+i++))) {
|
||||||
cerr << "<command line>: error: scope index expression is not constant: " << nkey << endl;
|
cerr << "<command line>: error: scope index expression is not constant: " << nkey << endl;
|
||||||
|
free(key);
|
||||||
|
free(value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
name_component_t tmp(lex_strings.make(nkey));
|
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);
|
val = make_unsized_octal(num);
|
||||||
else {
|
else {
|
||||||
cerr << "<command line>: error: value specify error for defparam: " << name << endl;
|
cerr << "<command line>: error: value specify error for defparam: " << name << endl;
|
||||||
|
free(key);
|
||||||
|
free(value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -185,6 +192,8 @@ void parm_to_defparam_list(const string¶m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
free(key);
|
||||||
|
free(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue