From 59afaec70f96a4a8c8048015b8a3e51d47892645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Fri, 12 Apr 2019 00:26:00 +0200 Subject: [PATCH] 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. --- src/Misc.cpp | 40 ---------------------------------- src/Misc.h | 14 ------------ src/lua/LuaScriptInterface.cpp | 12 +++++----- 3 files changed, 5 insertions(+), 61 deletions(-) diff --git a/src/Misc.cpp b/src/Misc.cpp index ec3e3fba6..e6813164c 100644 --- a/src/Misc.cpp +++ b/src/Misc.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) { diff --git a/src/Misc.h b/src/Misc.h index 9b876af62..adb40cddb 100644 --- a/src/Misc.h +++ b/src/Misc.h @@ -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); diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 9317dc0a0..393506186 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -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;