From e3a798740c2d64d0ac0dd4a08ebfec9d5a1c3cf8 Mon Sep 17 00:00:00 2001 From: amelia squires Date: Sat, 27 Sep 2025 04:11:49 -0500 Subject: leaks and memory fixes --- src/lua.c | 6 +++++- src/net/util.c | 10 +++++++--- src/thread.c | 11 ++++++++--- src/types/map.c | 8 ++++++-- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/lua.c b/src/lua.c index 2623e29..c51a906 100644 --- a/src/lua.c +++ b/src/lua.c @@ -51,7 +51,7 @@ int _stream_read(lua_State* L){ } lua_pushlstring(L, cont->c, cont->len); - free(cont); + str_free(cont); return 1; } @@ -185,6 +185,9 @@ void luaI_deepcopy(lua_State* src, lua_State* dest, enum deep_copy_flags flags){ case LUA_TBOOLEAN: lua_pushboolean(dest, lua_toboolean(src, -1)); break; + case LUA_TNIL: + lua_pushnil(dest); + break; case LUA_TSTRING:; size_t slen; const char* ss = lua_tolstring(src, -1, &slen); @@ -403,6 +406,7 @@ void luaI_copyvars(lua_State* from, lua_State* to){ env_table(from, x != 0); luaI_deepcopy(from, to, SKIP_GC | SKIP_LOCALS); + lua_pop(from, 1); int idx = lua_gettop(to); lua_pushglobaltable(to); int tidx = lua_gettop(to); diff --git a/src/net/util.c b/src/net/util.c index 78e3043..61cf1b7 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -341,7 +341,7 @@ int match_param(char* path, char* match, parray_t* arr){ case GET_KEY: if(path[pi] == '}'){ step = GET_VALUE; - name = calloc(pi - start, sizeof * name); + name = calloc(pi - start + 1, sizeof * name); memcpy(name, path + start + 1, pi - start - 1); start = mi; } @@ -352,7 +352,7 @@ int match_param(char* path, char* match, parray_t* arr){ if(match[mi] == '/'){ step = NORMAL; - char* out = calloc(mi - start, sizeof * out); + char* out = calloc(mi - start + 1, sizeof * out); memcpy(out, match + start, mi - start); parray_set(arr, name, out); free(name); @@ -410,6 +410,7 @@ uint64_t _mimetypes_len = 15; void parse_mimetypes(){ if(_mimetypes == NULL || _mimetypes_len == 0) return; + if(mime_type != NULL) return; mime_type = map_init(); FILE* fp = fopen(_mimetypes, "r"); @@ -436,9 +437,10 @@ void parse_mimetypes(){ if(line[i] == ' ' || line[i] == '\t' || line[i] == '\n'){ if(type_len == 0) continue; char* mtype_c = calloc(1024, sizeof * mtype); - strcpy(mtype_c, mtype); + mtype_c = strcpy(mtype_c, mtype); map_set(&mime_type, type, mtype_c); type_len = 0; + free(type); type = calloc(512, sizeof * type); } else { type[type_len] = line[i]; @@ -449,10 +451,12 @@ void parse_mimetypes(){ free(type); } + free(line); fclose(fp); } void _parse_mimetypes(){ + abort(); mime_type = map_init(); FILE* fp = fopen(MIMETYPES, "r"); char* buffer = calloc(1024, sizeof * buffer); diff --git a/src/thread.c b/src/thread.c index bb412ba..48ddba1 100644 --- a/src/thread.c +++ b/src/thread.c @@ -232,6 +232,9 @@ int _thread_await(lua_State* L){ lua_settop(info->L, ot); } + lua_pushnil(L); + lua_setglobal(L, "_locals"); + return info->return_count; } @@ -492,9 +495,11 @@ int l_buffer(lua_State* L){ luaI_tsetcf(L, meta_idx, "__index", l_buffer_index); luaI_tsetcf(L, meta_idx, "__gc", l_buffer_gc); - lua_getmetatable(L, 1); - int idx = lua_gettop(L); - luaI_tsetnil(L, idx, "__gc"); + if(use != 0){ + lua_getmetatable(L, 1); + int idx = lua_gettop(L); + luaI_tsetnil(L, idx, "__gc"); + } lua_pushvalue(L, meta_idx); lua_setmetatable(L, buffer_idx); diff --git a/src/types/map.c b/src/types/map.c index abf075c..80e072c 100644 --- a/src/types/map.c +++ b/src/types/map.c @@ -32,15 +32,19 @@ map_t* map_init(){ return map_initl(4); } +//TODO: make this better:3 void map_expand(map_t** _M){ map_t* M = *_M; map_t* remade = map_initl(M->mod * 4); for(int i = 0; i != M->mod; i++){ //what happens if the map_set calls map_regraph??? idk - if(M->M[i].used) - map_set(&remade, M->M[i].key->c, M->M[i].value); + if(M->M[i].used){ + map_set(&remade, M->M[i].key->c, M->M[i].value); + str_free(M->M[i].key); + } } + map_lclear(M); *_M = remade; } -- cgit v1.2.3