Commit 1ad8c45e authored by Vladimír Čunát's avatar Vladimír Čunát Committed by Tomas Krizek

lua module layers: avoid incorrect returns on errors

The layer functions are supposed to return the new state.
We've been lucky that errors are negative numbers and thus
they typically passed the (status & KR_STATE_FAIL) test.
parent 176b1c28
......@@ -81,12 +81,12 @@ static int l_ffi_defer(lua_State *L)
return uv_idle_start(check, l_ffi_resume_cb);
}
/** @internal Helper for calling the entrypoint. */
static inline int l_ffi_call(lua_State *L, int argc)
/** @internal Helper for calling the entrypoint, for kr_module functions. */
static inline int l_ffi_call_mod(lua_State *L, int argc)
{
int status = lua_pcall(L, argc, 1, 0);
if (status != 0) {
fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
kr_log_error("error: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
return kr_error(EIO);
}
......@@ -99,13 +99,21 @@ static inline int l_ffi_call(lua_State *L, int argc)
return status;
}
/** @internal Helper for calling the entrypoint, for kr_layer_api functions. */
static inline int l_ffi_call_layer(lua_State *L, int argc)
{
int ret = l_ffi_call_mod(L, argc);
/* The return codes are mixed at this point. We need to return KR_STATE_* */
return ret < 0 ? KR_STATE_FAIL : ret;
}
static int l_ffi_init(struct kr_module *module)
{
lua_State *L = l_ffi_preface(module, "init");
if (!L) {
return 0;
}
return l_ffi_call(L, 1);
return l_ffi_call_mod(L, 1);
}
static int l_ffi_deinit(struct kr_module *module)
......@@ -114,7 +122,7 @@ static int l_ffi_deinit(struct kr_module *module)
int ret = 0;
lua_State *L = module->lib;
if (l_ffi_preface(module, "deinit")) {
ret = l_ffi_call(L, 1);
ret = l_ffi_call_mod(L, 1);
}
module->lib = NULL;
/* Free the layer API wrapper (unconst it) */
......@@ -147,14 +155,14 @@ static int l_ffi_layer_begin(kr_layer_t *ctx)
{
LAYER_FFI_CALL(ctx, begin);
lua_pushlightuserdata(L, ctx->req);
return l_ffi_call(L, 2);
return l_ffi_call_layer(L, 2);
}
static int l_ffi_layer_reset(kr_layer_t *ctx)
{
LAYER_FFI_CALL(ctx, reset);
lua_pushlightuserdata(L, ctx->req);
return l_ffi_call(L, 2);
return l_ffi_call_layer(L, 2);
}
static int l_ffi_layer_finish(kr_layer_t *ctx)
......@@ -163,7 +171,7 @@ static int l_ffi_layer_finish(kr_layer_t *ctx)
LAYER_FFI_CALL(ctx, finish);
lua_pushlightuserdata(L, req);
lua_pushlightuserdata(L, req->answer);
return l_ffi_call(L, 3);
return l_ffi_call_layer(L, 3);
}
static int l_ffi_layer_consume(kr_layer_t *ctx, knot_pkt_t *pkt)
......@@ -174,7 +182,7 @@ static int l_ffi_layer_consume(kr_layer_t *ctx, knot_pkt_t *pkt)
LAYER_FFI_CALL(ctx, consume);
lua_pushlightuserdata(L, ctx->req);
lua_pushlightuserdata(L, pkt);
return l_ffi_call(L, 3);
return l_ffi_call_layer(L, 3);
}
static int l_ffi_layer_produce(kr_layer_t *ctx, knot_pkt_t *pkt)
......@@ -185,7 +193,7 @@ static int l_ffi_layer_produce(kr_layer_t *ctx, knot_pkt_t *pkt)
LAYER_FFI_CALL(ctx, produce);
lua_pushlightuserdata(L, ctx->req);
lua_pushlightuserdata(L, pkt);
return l_ffi_call(L, 3);
return l_ffi_call_layer(L, 3);
}
static int l_ffi_layer_checkout(kr_layer_t *ctx, knot_pkt_t *pkt, struct sockaddr *dst, int type)
......@@ -198,14 +206,14 @@ static int l_ffi_layer_checkout(kr_layer_t *ctx, knot_pkt_t *pkt, struct sockadd
lua_pushlightuserdata(L, pkt);
lua_pushlightuserdata(L, dst);
lua_pushboolean(L, type == SOCK_STREAM);
return l_ffi_call(L, 5);
return l_ffi_call_layer(L, 5);
}
static int l_ffi_layer_answer_finalize(kr_layer_t *ctx)
{
LAYER_FFI_CALL(ctx, answer_finalize);
lua_pushlightuserdata(L, ctx->req);
return l_ffi_call(L, 2);
return l_ffi_call_layer(L, 2);
}
#undef LAYER_FFI_CALL
......@@ -255,7 +263,7 @@ int ffimodule_register_lua(struct engine *engine, struct kr_module *module, cons
lua_getglobal(L, "require");
lua_pushfstring(L, "kres_modules.%s", name);
if (lua_pcall(L, 1, LUA_MULTRET, 0) != 0) {
fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
kr_log_error("error: %s\n", lua_tostring(L, -1));
lua_pop(L, 1);
return kr_error(ENOENT);
}
......
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