aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/libraries/Runnr.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libraries/Runnr.c')
-rw-r--r--src/libraries/Runnr.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/libraries/Runnr.c b/src/libraries/Runnr.c
index b775e19..72bc523 100644
--- a/src/libraries/Runnr.c
+++ b/src/libraries/Runnr.c
@@ -151,6 +151,9 @@ static char *_push_name(lua_State *L, char *q, int *idx) // Stack usage [-0, +1
151 return q; 151 return q;
152} 152}
153 153
154/* It's the callers job to stash things safely before returning from the Lua to C function call.
155 * Coz things like strings might go away after the stack is freed.
156 */
154int pull_lua(lua_State *L, int i, char *params, ...) // Stack usage - 157int pull_lua(lua_State *L, int i, char *params, ...) // Stack usage -
155 // if i is a table 158 // if i is a table
156 // [-n, +n, e] 159 // [-n, +n, e]
@@ -210,16 +213,15 @@ int pull_lua(lua_State *L, int i, char *params, ...) // Stack usage -
210 if (lua_isstring(L, j)) // Stack usage [-0, +0, -] 213 if (lua_isstring(L, j)) // Stack usage [-0, +0, -]
211 { 214 {
212 char **v = va_arg(vl, char **); 215 char **v = va_arg(vl, char **);
213 size_t len; 216
214 char *temp = (char *) lua_tolstring(L, j, &len); // Stack usage [-0, +0, m] 217 // We could strdup the string, but that causes leaks.
215 218 // The problem is that the caller doesn't know if we allocated or not,
216 len++; // Cater for the null at the end. 219 // since the incoming pointer could already be pointing to a default value.
217 *v = malloc(len); 220 // Lua says the string is valid until it's popped off the stack,
218 if (*v) 221 // and this is used only in calls to C functions from Lua.
219 { 222 // So just document that it's the callers job to stash it safely if needed after returning.
220 memcpy(*v, temp, len); 223 *v = (char *) lua_tostring(L, j);
221 n++; 224 n++;
222 }
223 } 225 }
224 break; 226 break;
225 } 227 }