From 281b71b14cdb30f742d766d760cad89a427e577f Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Sun, 21 Apr 2024 15:12:07 +0300 Subject: [PATCH] Addressed memory leak, and fixed issue relating to tarballs. --- include/taur.hpp | 3 ++- src/main.cpp | 29 +++++++++++++++++++++-------- src/taur.cpp | 12 ++++++++---- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/include/taur.hpp b/include/taur.hpp index 2b52284..207875f 100644 --- a/include/taur.hpp +++ b/include/taur.hpp @@ -11,6 +11,7 @@ #include #include "cpr/cpr.h" #include +#include "util.hpp" class Config; @@ -44,7 +45,7 @@ class TaurBackend { optional fetch_pkg(string pkg, bool returnGit); vector fetch_pkgs(vector pkgs, bool returnGit); bool remove_pkg(alpm_pkg_t *pkgs, bool ownTransaction = true); - bool remove_pkgs(alpm_list_t *pkgs); + bool remove_pkgs(alpm_list_smart_pointer &pkgs); bool handle_aur_depends(TaurPkg_t pkg, path out_path, vector localPkgs, bool useGit); bool build_pkg(string pkg_name, string extracted_path, bool alreadyprepared); bool update_all_aur_pkgs(string cacheDir, bool useGit); diff --git a/src/main.cpp b/src/main.cpp index 811a815..76b47d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -256,17 +256,21 @@ bool removePkg(alpm_list_t *pkgNames) { if (!pkgNames) return false; - alpm_list_t *ret = nullptr; + alpm_list_t *temp_ret = nullptr; alpm_list_t *regexQuery = nullptr; for (; pkgNames; pkgNames = pkgNames->next) regexQuery = alpm_list_add(regexQuery, (void *)((".*" + string((const char *)(pkgNames->data)) + ".*").c_str())); - if (alpm_db_search(alpm_get_localdb(config->handle), regexQuery, &ret) != 0) + alpm_list_free(regexQuery); + + if (alpm_db_search(alpm_get_localdb(config->handle), regexQuery, &temp_ret) != 0) return false; - size_t ret_length = alpm_list_count(ret); + alpm_list_smart_pointer ret(temp_ret, alpm_list_free); + + size_t ret_length = alpm_list_count(ret.get()); if (ret_length == 0) { log_println(LOG_ERROR, "No packages found!"); @@ -279,7 +283,7 @@ bool removePkg(alpm_list_t *pkgNames) { fmt::println("Choose packages to remove, (Seperate by spaces, type * to remove all):"); for (size_t i = 0; i < ret_length; i++) { - fmt::println("[{}] {}", i, alpm_pkg_get_name((alpm_pkg_t *)(alpm_list_nth(ret, i)->data))); + fmt::println("[{}] {}", i, alpm_pkg_get_name((alpm_pkg_t *)(alpm_list_nth(ret.get(), i)->data))); } string included; @@ -287,8 +291,8 @@ bool removePkg(alpm_list_t *pkgNames) { vector includedIndexes = split(included, ' '); - alpm_list_t *finalPackageList = nullptr; - alpm_list_t *finalPackageListStart = nullptr; + alpm_list_t *finalPackageList = nullptr; + alpm_list_t *finalPackageListStart = nullptr; if (included == "*") return backend->remove_pkgs(ret); @@ -300,18 +304,27 @@ bool removePkg(alpm_list_t *pkgNames) { if (includedIndex >= ret_length) continue; - finalPackageList = alpm_list_add(finalPackageList, alpm_list_nth(ret, includedIndex)->data); + finalPackageList = alpm_list_add(finalPackageList, alpm_list_nth(ret.get(), includedIndex)->data); if (finalPackageList != nullptr && finalPackageListStart == nullptr) finalPackageListStart = finalPackageList; } catch (std::invalid_argument const&) { log_println(LOG_WARN, "Invalid argument! Assuming all."); + + if (finalPackageListStart != nullptr) + alpm_list_free(finalPackageListStart); + else if (finalPackageList != nullptr) + alpm_list_free(finalPackageList); + return backend->remove_pkgs(ret); } } - return backend->remove_pkgs(finalPackageListStart); + // take control of the list and pass it to the smart pointer + alpm_list_smart_pointer finalList = make_list_smart_pointer(finalPackageListStart); + + return backend->remove_pkgs(finalList); } bool updateAll() { diff --git a/src/taur.cpp b/src/taur.cpp index 35a80b9..e2a2291 100644 --- a/src/taur.cpp +++ b/src/taur.cpp @@ -3,6 +3,7 @@ #include "util.hpp" #include "taur.hpp" #include "config.hpp" +#include namespace fs = std::filesystem; @@ -158,7 +159,7 @@ bool TaurBackend::remove_pkg(alpm_pkg_t *pkg, bool ownTransaction) { @param pkgs alpm list of alpm_pkg_t pointers to remove. @return success. */ -bool TaurBackend::remove_pkgs(alpm_list_t *pkgs) { +bool TaurBackend::remove_pkgs(alpm_list_smart_pointer &pkgs) { if (!pkgs) return false; @@ -167,7 +168,7 @@ bool TaurBackend::remove_pkgs(alpm_list_t *pkgs) { return false; } - size_t pkgs_length = alpm_list_count(pkgs); + size_t pkgs_length = alpm_list_count(pkgs.get()); if (pkgs_length == 0) { log_println(LOG_ERROR, "Couldn't find any packages!"); @@ -177,8 +178,8 @@ bool TaurBackend::remove_pkgs(alpm_list_t *pkgs) { return this->remove_pkg((alpm_pkg_t *)(pkgs->data), false) && commitTransactionAndRelease(); } - for (; pkgs; pkgs = pkgs->next) { - bool success = this->remove_pkg((alpm_pkg_t *)(pkgs->data), false); + for (alpm_list_t *pkgsGet = pkgs.get(); pkgsGet; pkgsGet = pkgsGet->next) { + bool success = this->remove_pkg((alpm_pkg_t *)(pkgsGet->data), false); if (!success) { alpm_trans_release(this->config.handle); return false; @@ -379,6 +380,9 @@ bool TaurBackend::update_all_aur_pkgs(string cacheDir, bool useGit) { string pkgFolder = cacheDir + '/' + onlinePkgs[i].name; sanitizeStr(pkgFolder); + if (!useGit) + std::filesystem::remove_all(pkgFolder); + bool downloadSuccess = this->download_pkg(onlinePkgs[i].url, pkgFolder); if (!downloadSuccess) {