aboutsummaryrefslogtreecommitdiff
path: root/ldblib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-10-12 14:51:28 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-10-12 14:51:28 -0300
commit30528049f1d11ea2854a6431e8e8524f83206559 (patch)
treef50e46758cb5673df7e6e456b1e94812a2c5050d /ldblib.c
parent9a89fb1c9dfeda4640780111f9e9437f08cfad88 (diff)
downloadlua-30528049f1d11ea2854a6431e8e8524f83206559.tar.gz
lua-30528049f1d11ea2854a6431e8e8524f83206559.tar.bz2
lua-30528049f1d11ea2854a6431e8e8524f83206559.zip
'lua_upvalueid' returns NULL on invalid upvalue index
Diffstat (limited to 'ldblib.c')
-rw-r--r--ldblib.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/ldblib.c b/ldblib.c
index 26058b50..5a326ade 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -281,25 +281,33 @@ static int db_setupvalue (lua_State *L) {
281** Check whether a given upvalue from a given closure exists and 281** Check whether a given upvalue from a given closure exists and
282** returns its index 282** returns its index
283*/ 283*/
284static int checkupval (lua_State *L, int argf, int argnup) { 284static void *checkupval (lua_State *L, int argf, int argnup, int *pnup) {
285 void *id;
285 int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */ 286 int nup = (int)luaL_checkinteger(L, argnup); /* upvalue index */
286 luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */ 287 luaL_checktype(L, argf, LUA_TFUNCTION); /* closure */
287 luaL_argcheck(L, (lua_getupvalue(L, argf, nup) != NULL), argnup, 288 id = lua_upvalueid(L, argf, nup);
288 "invalid upvalue index"); 289 if (pnup) {
289 return nup; 290 luaL_argcheck(L, id != NULL, argnup, "invalid upvalue index");
291 *pnup = nup;
292 }
293 return id;
290} 294}
291 295
292 296
293static int db_upvalueid (lua_State *L) { 297static int db_upvalueid (lua_State *L) {
294 int n = checkupval(L, 1, 2); 298 void *id = checkupval(L, 1, 2, NULL);
295 lua_pushlightuserdata(L, lua_upvalueid(L, 1, n)); 299 if (id != NULL)
300 lua_pushlightuserdata(L, id);
301 else
302 luaL_pushfail(L);
296 return 1; 303 return 1;
297} 304}
298 305
299 306
300static int db_upvaluejoin (lua_State *L) { 307static int db_upvaluejoin (lua_State *L) {
301 int n1 = checkupval(L, 1, 2); 308 int n1, n2;
302 int n2 = checkupval(L, 3, 4); 309 checkupval(L, 1, 2, &n1);
310 checkupval(L, 3, 4, &n2);
303 luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected"); 311 luaL_argcheck(L, !lua_iscfunction(L, 1), 1, "Lua function expected");
304 luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected"); 312 luaL_argcheck(L, !lua_iscfunction(L, 3), 3, "Lua function expected");
305 lua_upvaluejoin(L, 1, n1, 3, n2); 313 lua_upvaluejoin(L, 1, n1, 3, n2);