Skip to content

Commit

Permalink
Lua: Free Pref default string
Browse files Browse the repository at this point in the history
Store the Pref default string value and ensure this is freed both
when registering the pref and when not.

Use g_malloc0 to allocate Pref and avoid several init's.

Change-Id: I5f97a15d06068d7805f02f7c7feea61f9b2030f5
Reviewed-on: https://code.wireshark.org/review/12626
Reviewed-by: Guy Harris <[email protected]>
  • Loading branch information
stigbjorlykke authored and guyharris committed Dec 14, 2015
1 parent 6af3fa0 commit 5625b62
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions epan/wslua/wslua.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ typedef struct _wslua_pref_t {
option menu or combo box in
the preferences tab */
} enum_info; /**< for PREF_ENUM */
gchar* default_s; /**< default value for value.s */
} info; /**< display/text file information */

struct _wslua_pref_t* next;
Expand Down
12 changes: 7 additions & 5 deletions epan/wslua/wslua_pref.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,10 @@ static int new_pref(lua_State* L, pref_type_t type) {
const gchar* label = luaL_optstring(L,1,NULL);
const gchar* descr = luaL_optstring(L,3,"");

Pref pref = (wslua_pref_t *)g_malloc(sizeof(wslua_pref_t));
pref->name = NULL;
pref->label = label ? g_strdup(label) : NULL;
Pref pref = (wslua_pref_t *)g_malloc0(sizeof(wslua_pref_t));
pref->label = g_strdup(label);
pref->desc = g_strdup(descr);
pref->type = type;
pref->next = NULL;
pref->proto = NULL;

switch(type) {
case PREF_BOOL: {
Expand All @@ -123,6 +120,7 @@ static int new_pref(lua_State* L, pref_type_t type) {
case PREF_STRING: {
gchar* def = g_strdup(luaL_optstring(L,2,""));
pref->value.s = def;
pref->info.default_s = def;
break;
}
case PREF_ENUM: {
Expand Down Expand Up @@ -250,6 +248,8 @@ static int Pref__gc(lua_State* L) {
if (! pref->name) {
g_free(pref->label);
g_free(pref->desc);
if (pref->type == PREF_STRING)
g_free(pref->info.default_s);
g_free(pref);
}

Expand Down Expand Up @@ -360,6 +360,8 @@ WSLUA_METAMETHOD Prefs__newindex(lua_State* L) {
pref->label,
pref->desc,
(const char **)(&(pref->value.s)));
g_free(pref->info.default_s);
pref->info.default_s = NULL;
break;
case PREF_ENUM:
prefs_register_enum_preference(prefs_p->proto->prefs_module,
Expand Down

0 comments on commit 5625b62

Please sign in to comment.