Skip to content
Permalink
Browse files

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.
  • Loading branch information...
LBPHacker committed Apr 11, 2019
1 parent 1b2a525 commit 59afaec70f96a4a8c8048015b8a3e51d47892645
Showing with 5 additions and 61 deletions.
  1. +0 −40 src/Misc.cpp
  2. +0 −14 src/Misc.h
  3. +5 −7 src/lua/LuaScriptInterface.cpp
@@ -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)
{
@@ -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);
@@ -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,16 +2643,17 @@ 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;
}
}
}

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;

0 comments on commit 59afaec

Please sign in to comment.
You can’t perform that action at this time.