From 0f9f29019e1f42882096f649627be3c6e184f7da Mon Sep 17 00:00:00 2001 From: forever2k38 Date: Mon, 7 Sep 2026 16:54:12 +0200 Subject: fix: (most) memory leaks --- Makefile | 2 +- src/build.c | 2 -- src/download.c | 6 ++++++ src/package.c | 17 +++++++++++++++-- src/package.h | 6 +----- src/paths.c | 2 -- src/pow_lua.c | 5 +++-- src/repo.c | 7 +++++++ src/repo.h | 1 + src/source.c | 13 +++++++++++++ src/source.h | 12 ++++++++++++ 11 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 src/source.c create mode 100644 src/source.h diff --git a/Makefile b/Makefile index 90d1637..7dee997 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ RM = rm -rf CFLAGS=-Wall -Wpedantic LIBFLAGS=$(shell pkg-config --cflags --libs lua55) $(shell pkg-config --cflags --libs libarchive) $(shell pkg-config --cflags --libs libcurl) $(shell pkg-config --cflags --libs libb2) -DEBUG_CFLAGS=-g +DEBUG_CFLAGS=-g -fsanitize=leak SRC_DIR=src OUT_DIR=out diff --git a/src/build.c b/src/build.c index 9463136..5558720 100644 --- a/src/build.c +++ b/src/build.c @@ -29,8 +29,6 @@ int pow_package_build(pow_package *pkg) { return 1; } - printf("%s\n", tmp_path); - if ((err = chdir(tmp_path))) { printf("uh oh oops %d\n", err); diff --git a/src/download.c b/src/download.c index fb5746d..0554ee5 100644 --- a/src/download.c +++ b/src/download.c @@ -61,6 +61,8 @@ int pow_download_source(pow_package_source *src, const char *prefix) { if (pow_download_file(full_path, src->in)) { printf("failed to download sources, aborting\n"); + free(full_path); + return 1; } @@ -68,10 +70,14 @@ int pow_download_source(pow_package_source *src, const char *prefix) { printf("hash mismatch. something is going wrong with downloading the file " "or it may be tampered with.\n"); + free(full_path); + return 1; } printf("hash verification succeeded, going through."); + free(full_path); + return 0; } diff --git a/src/package.c b/src/package.c index ad7e4d0..6aeee65 100644 --- a/src/package.c +++ b/src/package.c @@ -139,12 +139,19 @@ int extract_package_info(lua_State *L, pow_package *pkg) { const char *hash = strdup(lua_tostring(L, -1)); + if (!hash) { + free(in); + free(out); + + printf("failed to strdup hash\n"); + + return 1; + } + src->hash = hash; pkg->source_count++; - printf("attempting to malloc\n"); - pkg->sources = realloc(pkg->sources, pkg->source_count * sizeof(pow_package_source *)); @@ -246,5 +253,11 @@ int pow_package_close(pow_package *pkg) { lua_close(pkg->L); + for (size_t i = 0; i > pkg->source_count; i++) { + pow_source_close(pkg->sources[i]); + } + + free(pkg); + return 0; } diff --git a/src/package.h b/src/package.h index 5d84800..c52a4a6 100644 --- a/src/package.h +++ b/src/package.h @@ -4,11 +4,7 @@ #include "lua.h" #include "stddef.h" -typedef struct { - const char *in; - const char *out; - const char *hash; -} pow_package_source; +#include "source.h" typedef struct { const char *name; diff --git a/src/paths.c b/src/paths.c index d4239ff..b8d7cd5 100644 --- a/src/paths.c +++ b/src/paths.c @@ -33,8 +33,6 @@ int pow_create_dir_for_package(char **path) { return 1; } - printf("%s\n", new_path); - *path = new_path; return 0; diff --git a/src/pow_lua.c b/src/pow_lua.c index cd71c87..7cf14b4 100644 --- a/src/pow_lua.c +++ b/src/pow_lua.c @@ -7,8 +7,6 @@ #include "string.h" int pow_lua_extract(lua_State *L) { - printf("extract called\n"); - int r; ssize_t size; @@ -93,7 +91,10 @@ int pow_lua_extract(lua_State *L) { } snprintf(full, need, "%s/%s", dest, cur); + archive_entry_set_pathname(entry, full); + + free(full); } r = archive_read_extract(a, entry, 0); diff --git a/src/repo.c b/src/repo.c index 846136c..9e326e7 100644 --- a/src/repo.c +++ b/src/repo.c @@ -69,12 +69,17 @@ int pow_repo_get_package(pow_repo **repos, size_t repo_count, pow_package *pkg, query); free(package_path); + free(full_path); return 1; } + free(package_path); + pow_package_open(pkg, full_path); + free(full_path); + closedir(dp); } @@ -103,5 +108,7 @@ int pow_repo_check_package(pow_repo **repos, size_t repo_count, "Version: %s\n", pkg->name, pkg->version); + pow_package_close(pkg); + return 0; } diff --git a/src/repo.h b/src/repo.h index f828def..0cc8a31 100644 --- a/src/repo.h +++ b/src/repo.h @@ -16,4 +16,5 @@ int pow_repo_check_package(pow_repo **repos, size_t repo_count, int pow_repo_get_package(pow_repo **repos, size_t repo_count, pow_package *pkg, const char *query); + #endif diff --git a/src/source.c b/src/source.c new file mode 100644 index 0000000..5b6aa9b --- /dev/null +++ b/src/source.c @@ -0,0 +1,13 @@ +#include "source.h" + +#include "stdlib.h" + +int pow_source_close(pow_package_source *src) { + free(src->hash); + free(src->in); + free(src->out); + + free(src); + + return 0; +} diff --git a/src/source.h b/src/source.h new file mode 100644 index 0000000..84dad4b --- /dev/null +++ b/src/source.h @@ -0,0 +1,12 @@ +#ifndef POW_SOURCE_H +#define POW_SOURCE_H + +typedef struct { + const char *in; + const char *out; + const char *hash; +} pow_package_source; + +int pow_source_close(pow_package_source *src); + +#endif -- cgit v1.3