summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-10-22 01:10:15 +0200
committerwm4 <wm4@nowhere>2014-10-22 01:10:15 +0200
commit65ba6f9f57a7eff58383cc7b7aa790aa8381e60b (patch)
tree827888da89e1e363fcc759a8a36c1c9bf95addd6
parent2e81698d2809836d4cd7f754a78598e7bdf96c0b (diff)
downloadmpv-65ba6f9f57a7eff58383cc7b7aa790aa8381e60b.tar.bz2
mpv-65ba6f9f57a7eff58383cc7b7aa790aa8381e60b.tar.xz
lua: don't let temporary values take the place of arguments
Because Lua is so terrible, it's easy to confuse temporary values pushed to the Lua stack with arguments if the arguments are checked after that. Add a hack that should fix this.
-rw-r--r--player/lua.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/player/lua.c b/player/lua.c
index cfb7cc22fc..d147d2c687 100644
--- a/player/lua.c
+++ b/player/lua.c
@@ -94,6 +94,15 @@ static int mp_cpcall (lua_State *L, lua_CFunction func, void *ud)
#define mp_lua_len lua_rawlen
#endif
+// Ensure that the given argument exists, even if it's nil. Can be used to
+// avoid confusing the last missing optional arg with the first temporary value
+// pushed to the stack.
+static void mp_lua_optarg(lua_State *L, int arg)
+{
+ while (arg < lua_gettop(L))
+ lua_pushnil(L);
+}
+
static int destroy_crap(lua_State *L)
{
void **data = luaL_checkudata(L, 1, "ohthispain");
@@ -878,6 +887,7 @@ static int script_get_property_native(lua_State *L)
{
struct script_ctx *ctx = get_ctx(L);
const char *name = luaL_checkstring(L, 1);
+ mp_lua_optarg(L, 2);
void *tmp = mp_lua_PITA(L);
mpv_node node;
@@ -929,6 +939,7 @@ static int script_raw_unobserve_property(lua_State *L)
static int script_command_native(lua_State *L)
{
struct script_ctx *ctx = get_ctx(L);
+ mp_lua_optarg(L, 2);
struct mpv_node node;
struct mpv_node result;
void *tmp = mp_lua_PITA(L);
@@ -1171,9 +1182,9 @@ extern char **environ;
static int script_subprocess(lua_State *L)
{
- void *tmp = mp_lua_PITA(L);
struct script_ctx *ctx = get_ctx(L);
luaL_checktype(L, 1, LUA_TTABLE);
+ void *tmp = mp_lua_PITA(L);
resume_all(ctx);
@@ -1294,6 +1305,7 @@ done:
static int script_parse_json(lua_State *L)
{
+ mp_lua_optarg(L, 2);
void *tmp = mp_lua_PITA(L);
char *text = talloc_strdup(tmp, luaL_checkstring(L, 1));
bool trail = lua_toboolean(L, 2);