Commit 316bd16e authored by David Milligan's avatar David Milligan
Browse files

lua.mo: make a copy of long-lived strings that we give to ML backends, because...

lua.mo: make a copy of long-lived strings that we give to ML backends, because lua may move strings around on us, and fix some const correctness issues

--HG--
branch : lua
parent a1fbf453a69b
......@@ -107,6 +107,14 @@ static int string_ends_with(const char *source, const char *ending)
return !strcmp(source + strlen(source) - strlen(ending), ending);
}
char * copy_string(const char * str)
{
size_t len = strlen(str) + 1;
char * copy = malloc(sizeof(char) * len);
if(copy) strncpy(copy,str,len);
return copy;
}
/***
Event Handlers.
......
......@@ -56,7 +56,7 @@ const char * name = lua_tostring(L, index)
#define LUA_PARAM_STRING_OPTIONAL(name, index, default) const char * name = (index <= lua_gettop(L) && lua_isstring(L, index)) ? lua_tostring(L, index) : default
#define LUA_FIELD_STRING(field, default) lua_getfield(L, -1, field) == LUA_TSTRING ? lua_tostring(L, -1) : default; lua_pop(L, 1)
#define LUA_FIELD_STRING(field, default) lua_getfield(L, -1, field) == LUA_TSTRING ? copy_string(lua_tostring(L, -1)) : copy_string(default); lua_pop(L, 1)
#define LUA_FIELD_INT(field, default) lua_getfield(L, -1, field) == LUA_TNUMBER ? lua_tointeger(L, -1) : default; lua_pop(L, 1)
#define LUA_FIELD_BOOL(field, default) lua_getfield(L, -1, field) == LUA_TBOOLEAN ? lua_toboolean(L, -1) : default; lua_pop(L, 1)
......@@ -90,6 +90,7 @@ struct script_menu_entry
int run_in_separate_task;
};
char * copy_string(const char * str);
int docall(lua_State *L, int narg, int nres);
int lua_take_semaphore(lua_State * L, int timeout, struct semaphore ** assoc_semaphore);
......
......@@ -216,8 +216,10 @@ static int luaCB_dryos_newindex(lua_State * L)
}
else if(!strcmp(key, "prefix"))
{
static char prefix[8];
LUA_PARAM_STRING(new_prefix, 3);
file_prefix_set(new_prefix);
strncpy(prefix, new_prefix, 7);
file_prefix_set(prefix);
}
else
{
......@@ -316,14 +318,6 @@ static int luaCB_directory_files(lua_State * L)
return 1;
}
static char * copy_string(const char * str)
{
size_t len = strlen(str) + 1;
char * copy = malloc(sizeof(char) * len);
if(copy) strncpy(copy,str,len);
return copy;
}
static int luaCB_directory_index(lua_State * L)
{
if(!lua_istable(L, 1)) return luaL_argerror(L, 1, "expected table");
......
......@@ -237,6 +237,12 @@ static int luaCB_lvinfo_index(lua_State * L)
return 1;
}
static void set_string(char ** entry, const char * value)
{
if(*entry) free(*entry);
*entry = copy_string(value);
}
static int luaCB_lvinfo_newindex(lua_State * L)
{
if(!lua_isuserdata(L, 1)) return luaL_argerror(L, 1, NULL);
......@@ -263,12 +269,12 @@ static int luaCB_lvinfo_newindex(lua_State * L)
else if(!strcmp(key, "name"))
{
LUA_PARAM_STRING(value, 3);
item->name = value;
set_string(&(item->name),value);
}
else if(!strcmp(key, "value"))
{
LUA_PARAM_STRING(value, 3);
item->value = value;
set_string(&(item->value),value);
}
else if(!strcmp(key, "background"))
{
......
......@@ -305,7 +305,9 @@ static int luaCB_menu_set(lua_State * L)
else
{
LUA_PARAM_STRING(value, 3);
lua_pushboolean(L, menu_set_str_value_from_script(menu, entry, value, -1));
char * copy = copy_string(value);
lua_pushboolean(L, menu_set_str_value_from_script(menu, entry, copy, -1));
free(copy);
}
return 1;
}
......@@ -468,6 +470,14 @@ static int luaCB_menu_instance_index(lua_State * L)
return 1;
}
// we can't really maintain const correctness here because of the struct definition, const is discarded to avoid compiler warnings
// we will only ever put dynamically created non-const strings in our structs for ML backends, so it's okay to discard the const
static void set_string(const char ** entry, const char * value)
{
if(*entry) free((char *)*entry);
*entry = copy_string(value);
}
static int luaCB_menu_instance_newindex(lua_State * L)
{
if(!lua_isuserdata(L, 1)) return luaL_argerror(L, 1, NULL);
......@@ -488,9 +498,9 @@ static int luaCB_menu_instance_newindex(lua_State * L)
script_entry->menu_value = value;
}
}
else if(!strcmp(key, "name")) { LUA_PARAM_STRING(value, 3); script_entry->menu_entry->name = value; }
else if(!strcmp(key, "help")) { LUA_PARAM_STRING(value, 3); script_entry->menu_entry->help = value; }
else if(!strcmp(key, "help2")) { LUA_PARAM_STRING(value, 3); script_entry->menu_entry->help2 = value; }
else if(!strcmp(key, "name")) { LUA_PARAM_STRING(value, 3); set_string(&(script_entry->menu_entry->name),value); }
else if(!strcmp(key, "help")) { LUA_PARAM_STRING(value, 3); set_string(&(script_entry->menu_entry->help),value); }
else if(!strcmp(key, "help2")) { LUA_PARAM_STRING(value, 3); set_string(&(script_entry->menu_entry->help2),value); }
else if(!strcmp(key, "advanced")) { LUA_PARAM_INT(value, 3); script_entry->menu_entry->advanced = value; }
else if(!strcmp(key, "depends_on")) { LUA_PARAM_INT(value, 3); script_entry->menu_entry->depends_on = value; }
else if(!strcmp(key, "edit_mode")) { LUA_PARAM_INT(value, 3); script_entry->menu_entry->edit_mode = value; }
......@@ -759,6 +769,12 @@ static int luaCB_menu_remove(lua_State * L)
const char * parent = LUA_FIELD_STRING("parent", "LUA");
menu_remove(parent, script_entry->menu_entry, 1);
luaL_unref(L, LUA_REGISTRYINDEX, script_entry->self_ref);
if(script_entry->menu_entry->name) free((char*)script_entry->menu_entry->name);
script_entry->menu_entry->name = NULL;
if(script_entry->menu_entry->help) free((char*)script_entry->menu_entry->help);
script_entry->menu_entry->help = NULL;
if(script_entry->menu_entry->help2) free((char*)script_entry->menu_entry->help2);
script_entry->menu_entry->help2 = NULL;
}
else
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment