BUG/MINOR: hlua: Fix memory leaks on error path when registering a service

When an error occurred in hlua_register_service(), the allocated lua
function and keyword must be released to avoid memory leaks.

This patch depends on "MINOR: hlua: Add function to release a lua
function". It may be backported in all stable versions.
This commit is contained in:
Christopher Faulet 2021-04-12 15:11:44 +02:00
parent 4fc9da01d2
commit 5c028d7f9d

View File

@ -7847,7 +7847,7 @@ __LJMP static int hlua_register_service(lua_State *L)
const char *env; const char *env;
int ref; int ref;
int len; int len;
struct hlua_function *fcn; struct hlua_function *fcn = NULL;
struct buffer *trash; struct buffer *trash;
struct action_kw *akw; struct action_kw *akw;
@ -7879,16 +7879,16 @@ __LJMP static int hlua_register_service(lua_State *L)
/* Allocate and fill the sample fetch keyword struct. */ /* Allocate and fill the sample fetch keyword struct. */
akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
if (!akl) if (!akl)
WILL_LJMP(luaL_error(L, "Lua out of memory error.")); goto alloc_error;
fcn = new_hlua_function(); fcn = new_hlua_function();
if (!fcn) if (!fcn)
WILL_LJMP(luaL_error(L, "Lua out of memory error.")); goto alloc_error;
/* Fill fcn. */ /* Fill fcn. */
len = strlen("<lua.>") + strlen(name) + 1; len = strlen("<lua.>") + strlen(name) + 1;
fcn->name = calloc(1, len); fcn->name = calloc(1, len);
if (!fcn->name) if (!fcn->name)
WILL_LJMP(luaL_error(L, "Lua out of memory error.")); goto alloc_error;
snprintf((char *)fcn->name, len, "<lua.%s>", name); snprintf((char *)fcn->name, len, "<lua.%s>", name);
fcn->function_ref[hlua_state_id] = ref; fcn->function_ref[hlua_state_id] = ref;
@ -7899,7 +7899,7 @@ __LJMP static int hlua_register_service(lua_State *L)
len = strlen("lua.") + strlen(name) + 1; len = strlen("lua.") + strlen(name) + 1;
akl->kw[0].kw = calloc(1, len); akl->kw[0].kw = calloc(1, len);
if (!akl->kw[0].kw) if (!akl->kw[0].kw)
WILL_LJMP(luaL_error(L, "Lua out of memory error.")); goto alloc_error;
snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
@ -7908,9 +7908,14 @@ __LJMP static int hlua_register_service(lua_State *L)
akl->kw[0].parse = action_register_service_tcp; akl->kw[0].parse = action_register_service_tcp;
else if (strcmp(env, "http") == 0) else if (strcmp(env, "http") == 0)
akl->kw[0].parse = action_register_service_http; akl->kw[0].parse = action_register_service_http;
else else {
release_hlua_function(fcn);
if (akl)
ha_free((char **)&(akl->kw[0].kw));
ha_free(&akl);
WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
"'tcp' or 'http' are expected.", env)); "'tcp' or 'http' are expected.", env));
}
akl->kw[0].match_pfx = 0; akl->kw[0].match_pfx = 0;
akl->kw[0].private = fcn; akl->kw[0].private = fcn;
@ -7922,6 +7927,12 @@ __LJMP static int hlua_register_service(lua_State *L)
service_keywords_register(akl); service_keywords_register(akl);
return 0; return 0;
alloc_error:
release_hlua_function(fcn);
ha_free(&akl);
WILL_LJMP(luaL_error(L, "Lua out of memory error."));
return 0; /* Never reached */
} }
/* This function initialises Lua cli handler. It copies the /* This function initialises Lua cli handler. It copies the