Skip to content

Commit

Permalink
[Fix] Fix memory leak in lua_new_text invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Nov 1, 2024
1 parent 5185512 commit 80cb50d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
5 changes: 2 additions & 3 deletions src/lua/lua_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ static inline int lua_absindex(lua_State *L, int i)
#define LUA_PUBLIC_FUNCTION_DEF(class, name) int lua_##class##_##name(lua_State *L)
#define LUA_INTERFACE_DEF(class, name) \
{ \
#name, lua_##class##_##name \
}
#name, lua_##class##_##name}

extern const luaL_reg null_reg[];

Expand Down Expand Up @@ -281,7 +280,7 @@ struct rspamd_lua_text *lua_check_text_or_string(lua_State *L, int pos);
* @return
*/
struct rspamd_lua_text *lua_new_text(lua_State *L, const char *start,
gsize len, gboolean own);
gsize len, gboolean allocate_memory);
/**
* Create new text object from task pool if allocation is needed
* @param task
Expand Down
12 changes: 7 additions & 5 deletions src/lua/lua_compress.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*-
* Copyright 2021 Vsevolod Stakhov
/*
* Copyright 2024 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -504,7 +504,8 @@ lua_zstd_compress_stream(lua_State *L)
return lua_zstd_push_error(L, err);
}

lua_new_text(L, onb.dst, onb.pos, TRUE);
t = lua_new_text(L, onb.dst, onb.pos, FALSE);
t->flags |= RSPAMD_TEXT_FLAG_OWN;

return 1;
}
Expand Down Expand Up @@ -598,7 +599,8 @@ lua_zstd_decompress_stream(lua_State *L)
return lua_zstd_push_error(L, err);
}

lua_new_text(L, onb.dst, onb.pos, TRUE);
t = lua_new_text(L, onb.dst, onb.pos, FALSE);
t->flags |= RSPAMD_TEXT_FLAG_OWN;

return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lua/lua_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,14 @@ lua_check_text_or_string(lua_State *L, int pos)
}

struct rspamd_lua_text *
lua_new_text(lua_State *L, const char *start, gsize len, gboolean own)
lua_new_text(lua_State *L, const char *start, gsize len, gboolean allocate_memory)
{
struct rspamd_lua_text *t;

t = lua_newuserdata(L, sizeof(*t));
t->flags = 0;

if (own) {
if (allocate_memory) {
char *storage;

if (len > 0) {
Expand Down
11 changes: 9 additions & 2 deletions src/lua/lua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,12 @@ lua_util_encode_base64(lua_State *L)
}

if (out != NULL) {
lua_new_text(L, out, outlen, TRUE);
/*
* Manually set OWN flag, as `lua_new_text` will allocate another chunk of memory,
* and we will have memory leak of the memory allocated by `rspamd_encode_base64_fold`
*/
t = lua_new_text(L, out, outlen, FALSE);
t->flags = RSPAMD_TEXT_FLAG_OWN;
}
else {
lua_pushnil(L);
Expand Down Expand Up @@ -1650,7 +1655,9 @@ lua_util_transliterate(lua_State *L)

gsize outlen;
char *transliterated = rspamd_utf8_transliterate(t->start, t->len, &outlen);
lua_new_text(L, transliterated, outlen, TRUE);

t = lua_new_text(L, transliterated, outlen, FALSE);
t->flags = RSPAMD_TEXT_FLAG_OWN;

return 1;
}
Expand Down

0 comments on commit 80cb50d

Please sign in to comment.