]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
auth: db-lua - Fix user iteration
authorAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 1 Jul 2020 11:49:36 +0000 (14:49 +0300)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Fri, 3 Jul 2020 06:56:42 +0000 (09:56 +0300)
The old code did not leave the stack empty after finishing up,
that would lead into stack being left dirty and accumulating
per each call.

src/auth/db-lua.c

index 75c19eae1aeb186f01b78f380acc189314043ad4..0651d1fff5511ecf178e696e457a2d4b865bf972 100644 (file)
@@ -746,23 +746,38 @@ auth_lua_call_userdb_iterate_init(struct dlua_script *script, struct auth_reques
 
        p_array_init(&actx->users, pool, 8);
 
-       lua_pushvalue(script->L, -1);
+       /* stack is now
+               table */
+
+       /* see lua_next documentation */
        lua_pushnil(script->L);
        while (lua_next(script->L, -2) != 0) {
-               lua_pushvalue(script->L, -2);
+               /* stack is now
+                       value
+                       key
+                       table */
                if (!lua_isstring(script->L, -1)) {
                        e_error(authdb_event(req),
                                "db-lua: Value is not string");
                        actx->ctx.failed = TRUE;
-                       lua_pop(script->L, 1);
+                       lua_pop(script->L, 3);
                        lua_gc(script->L, LUA_GCCOLLECT, 0);
+                       i_assert(lua_gettop(script->L) == 0);
                        return &actx->ctx;
                }
-               const char *str = p_strdup(pool, lua_tostring(script->L, -2));
+               const char *str = p_strdup(pool, lua_tostring(script->L, -1));
                array_push_back(&actx->users, &str);
-               lua_pop(script->L, 2);
+               lua_pop(script->L, 1);
+               /* stack is now
+                       key
+                       table */
        }
 
+       /* stack is now
+               table
+       */
+
+       lua_pop(script->L, 1);
        lua_gc(script->L, LUA_GCCOLLECT, 0);
        i_assert(lua_gettop(script->L) == 0);