From e5f0ac8decc8fce795550160da3bb531d53e3ced Mon Sep 17 00:00:00 2001 From: GreekStapler Date: Thu, 2 Feb 2023 19:29:34 +0000 Subject: [PATCH] fix: Plug memory leaks and fix strings that were not null terminated. --- src/package/vieter_package.c | 7 +++++-- test/package/test_package.c | 11 +++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/package/vieter_package.c b/src/package/vieter_package.c index bec0bf5..4bce4f5 100644 --- a/src/package/vieter_package.c +++ b/src/package/vieter_package.c @@ -44,7 +44,7 @@ vieter_package *vieter_package_init() { vieter_package_error vieter_package_read_archive(vieter_package *pkg, const char *pkg_path) { struct archive *a = archive_read_new(); - struct archive_entry *entry = archive_entry_new(); + struct archive_entry *entry; // These three are the most commonly used compression methods archive_read_support_filter_zstd(a); @@ -88,8 +88,9 @@ vieter_package_error vieter_package_read_archive(vieter_package *pkg, if (strcmp(path_name, ".PKGINFO") == 0) { // Read data of file into memory buffer int size = archive_entry_size(entry); - char *buf = malloc(size); + char *buf = malloc(size + 1); archive_read_data(a, buf, size); + buf[size] = '\0'; // Parse package vieter_package_info string into a struct pkg_info = vieter_package_info_init(); @@ -248,6 +249,8 @@ char *vieter_package_to_description(vieter_package *pkg) { strcat(description, "\n\n"); + free(aux); + return description; } diff --git a/test/package/test_package.c b/test/package/test_package.c index d4dd981..bcefdf6 100644 --- a/test/package/test_package.c +++ b/test/package/test_package.c @@ -8,8 +8,9 @@ void test_info_parse() { size_t size = ftell(f); fflush(stdout); rewind(f); - char *pkg_info_str = malloc(size); + char *pkg_info_str = malloc(size + 1); fread(pkg_info_str, 1, size, f); + pkg_info_str[size] = '\0'; fclose(f); vieter_package_info *pkg_info = vieter_package_info_init(); @@ -36,6 +37,9 @@ void test_info_parse() { TEST_CHECK(!strcmp(pkg_info->optdepends->array[0], "test7")); TEST_CHECK(!strcmp(pkg_info->makedepends->array[0], "xorg-xcursorgen")); TEST_CHECK(!strcmp(pkg_info->checkdepends->array[0], "test8")); + + free(pkg_info_str); + vieter_package_info_free(pkg_info); } void test_pkg_read_archive_files() { @@ -73,13 +77,16 @@ void test_pkg_read_archive_desc() { fseek(f, 0, SEEK_END); size_t size = ftell(f); rewind(f); - char *desc = malloc(size); + char *desc = malloc(size + 1); fread(desc, 1, size, f); + desc[size] = '\0'; fclose(f); TEST_CHECK(!strcmp(description, desc)); vieter_package_free(&pkg); + free(description); + free(desc); } TEST_LIST = {