Fix Element::Identifier assignment leak (closes #205)

I didn't use 'fixes' because this doesn't strictly fix that specific
bug (I think?). Anyway, it does remove strdup which is mentioned in the
issue.

Also fix elem.free not allowing DEFAULTFOO_PT_STUFF and similar to
be freed.
This commit is contained in:
Tamás Bálint Misius 2019-04-12 00:26:00 +02:00
parent 1b2a52501d
commit 59afaec70f
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
3 changed files with 5 additions and 61 deletions

View File

@ -36,46 +36,6 @@ float restrict_flt(float f, float min, float max) //TODO Inline or macro or some
return f;
}
char *mystrdup(const char *s)
{
char *x;
if (s)
{
x = (char*)malloc(strlen(s)+1);
strcpy(x, s);
return x;
}
return NULL;
}
void strlist_add(struct strlist **list, char *str)
{
struct strlist *item = (struct strlist*)malloc(sizeof(struct strlist));
item->str = mystrdup(str);
item->next = *list;
*list = item;
}
int strlist_find(struct strlist **list, char *str)
{
struct strlist *item;
for (item=*list; item; item=item->next)
if (!strcmp(item->str, str))
return 1;
return 0;
}
void strlist_free(struct strlist **list)
{
struct strlist *item;
while (*list)
{
item = *list;
*list = (*list)->next;
free(item);
}
}
const static char hex[] = "0123456789ABCDEF";
void strcaturl(char *dst, char *src)
{

View File

@ -18,20 +18,6 @@ unsigned clamp_flt(float f, float min, float max);
float restrict_flt(float f, float min, float max);
char *mystrdup(const char *s);
struct strlist
{
char *str;
struct strlist *next;
};
void strlist_add(struct strlist **list, char *str);
int strlist_find(struct strlist **list, char *str);
void strlist_free(struct strlist **list);
void save_presets(int do_update);
void load_presets(void);

View File

@ -2632,9 +2632,6 @@ int LuaScriptInterface::elements_allocate(lua_State * l)
if (!luacon_sim->elements[i].Enabled)
{
newID = i;
luacon_sim->elements[i] = Element();
luacon_sim->elements[i].Enabled = true;
luacon_sim->elements[i].Identifier = mystrdup(identifier.c_str());
break;
}
}
@ -2646,9 +2643,6 @@ int LuaScriptInterface::elements_allocate(lua_State * l)
if (!luacon_sim->elements[i].Enabled)
{
newID = i;
luacon_sim->elements[i] = Element();
luacon_sim->elements[i].Enabled = true;
luacon_sim->elements[i].Identifier = mystrdup(identifier.c_str());
break;
}
}
@ -2656,6 +2650,10 @@ int LuaScriptInterface::elements_allocate(lua_State * l)
if (newID != -1)
{
luacon_sim->elements[newID] = Element();
luacon_sim->elements[newID].Enabled = true;
luacon_sim->elements[newID].Identifier = identifier;
lua_getglobal(l, "elements");
lua_pushinteger(l, newID);
lua_setfield(l, -2, identifier.c_str());
@ -2876,7 +2874,7 @@ int LuaScriptInterface::elements_free(lua_State * l)
return luaL_error(l, "Invalid element");
ByteString identifier = luacon_sim->elements[id].Identifier;
if(identifier.BeginsWith("DEFAULT"))
if(identifier.BeginsWith("DEFAULT_PT_"))
return luaL_error(l, "Cannot free default elements");
luacon_sim->elements[id].Enabled = false;