Skip to content

Commit

Permalink
ruleset: improve error handling in aux_todialreq
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Nov 22, 2024
1 parent 6f682bf commit b30773a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/ruleset.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static int ruleset_luainit(lua_State *restrict L)
const char *strings[] = {
ERR_MEMORY,
ERR_BAD_REGISTRY,
ERR_INVALID_ROUTE,
ERR_INVALID_INVOKE,
ERR_NOT_ASYNC_ROUTINE,
};
const int nstrings = (int)ARRAY_SIZE(strings);
Expand Down
10 changes: 8 additions & 2 deletions src/ruleset/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "api.h"

#include "net/addr.h"
#include "utils/debug.h"
#include "utils/minmax.h"
#include "utils/serialize.h"

Expand Down Expand Up @@ -31,10 +32,15 @@ static int api_invoke(lua_State *restrict L)
{
size_t len;
const char *code = luaL_checklstring(L, 1, &len);
(void)luaL_checkstring(L, 2);
const int n = lua_gettop(L) - 1;
struct dialreq *req = aux_todialreq(L, n);
if (!aux_todialreq(L, n)) {
lua_pushliteral(L, ERR_INVALID_INVOKE);
return lua_error(L);
}
struct dialreq *req = lua_touserdata(L, -1);
if (req == NULL) {
lua_pushliteral(L, ERR_INVALID_ROUTE);
lua_pushliteral(L, ERR_INVALID_INVOKE);
return lua_error(L);
}
struct ruleset *restrict r = aux_getruleset(L);
Expand Down
8 changes: 6 additions & 2 deletions src/ruleset/await.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,13 @@ static int await_invoke(lua_State *restrict L)
size_t len;
const char *code = luaL_checklstring(L, 1, &len);
const int n = lua_gettop(L) - 1;
struct dialreq *req = aux_todialreq(L, n);
if (!aux_todialreq(L, n)) {
lua_pushliteral(L, ERR_INVALID_INVOKE);
return lua_error(L);
}
struct dialreq *req = lua_touserdata(L, -1);
if (req == NULL) {
lua_pushliteral(L, ERR_INVALID_ROUTE);
lua_pushliteral(L, ERR_INVALID_INVOKE);
return lua_error(L);
}
struct ruleset *restrict r = aux_getruleset(L);
Expand Down
26 changes: 13 additions & 13 deletions src/ruleset/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,52 +86,52 @@ int aux_format_addr(lua_State *restrict L)
return 1;
}

struct dialreq *aux_todialreq(lua_State *restrict L, const int n)
bool aux_todialreq(lua_State *restrict L, const int n)
{
/* lua stack: ... addr proxyN ... proxy1 */
if (n < 1) {
lua_pushlightuserdata(L, NULL);
return NULL;
return true;
}
if (lua_isnil(L, -1)) {
lua_pop(L, n);
lua_pushlightuserdata(L, NULL);
return true;
}
struct dialreq *req = dialreq_new((size_t)(n - 1));
if (req == NULL) {
LOGOOM();
lua_pop(L, n);
lua_pushlightuserdata(L, NULL);
return NULL;
return false;
}
size_t len;
for (int i = 1; i < n; i++) {
const char *s = lua_tolstring(L, -i, &len);
if (s == NULL) {
dialreq_free(req);
lua_pop(L, n);
lua_pushlightuserdata(L, NULL);
return NULL;
return false;
}
if (!dialreq_addproxy(req, s, len)) {
dialreq_free(req);
lua_pop(L, n);
lua_pushlightuserdata(L, NULL);
return NULL;
return false;
}
}
const char *s = lua_tolstring(L, -n, &len);
if (s == NULL) {
dialreq_free(req);
lua_pop(L, n);
lua_pushlightuserdata(L, NULL);
return NULL;
return false;
}
if (!dialaddr_parse(&req->addr, s, len)) {
dialreq_free(req);
lua_pop(L, n);
lua_pushlightuserdata(L, NULL);
return NULL;
return false;
}
lua_pop(L, n);
lua_pushlightuserdata(L, req);
return req;
return true;
}

int aux_traceback(lua_State *restrict L)
Expand Down
6 changes: 3 additions & 3 deletions src/ruleset/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ enum ruleset_ridx {

#define ERR_MEMORY "out of memory"
#define ERR_BAD_REGISTRY "Lua registry is corrupted"
#define ERR_INVALID_ROUTE "unable to parse route"
#define ERR_INVALID_INVOKE "invalid invocation target"
#define ERR_NOT_ASYNC_ROUTINE "not in asynchronous routine"

struct ruleset *aux_getruleset(lua_State *L);
Expand All @@ -43,8 +43,8 @@ const char *aux_reader(lua_State *L, void *ud, size_t *sz);
/* [-1, +1, v] */
int aux_format_addr(lua_State *L);

/* [-n, +1, -] */
struct dialreq *aux_todialreq(lua_State *L, int n);
/* [-n, +(0|1), -] */
bool aux_todialreq(lua_State *L, int n);

int aux_traceback(lua_State *L);

Expand Down
3 changes: 1 addition & 2 deletions src/ruleset/cfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ int cfunc_request(lua_State *restrict L)
if (n < 1) {
return 0;
}
struct dialreq *req = aux_todialreq(L, n);
if (req == NULL) {
if (!aux_todialreq(L, n)) {
LOGW_F("ruleset.%s `%s': invalid return", func, request);
}
return 1;
Expand Down

0 comments on commit b30773a

Please sign in to comment.