libhelper: fixup memory leak

This commit is contained in:
2025-07-26 10:28:17 +03:00
parent 7017cfc9a4
commit 40260d5ae3
8 changed files with 34 additions and 17 deletions

View File

@@ -20,8 +20,9 @@ project(pmt VERSION 1.0.0)
# Set compiler flags
add_compile_options(-Wall -Werror)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
if(${CMAKE_BUILD_TYPE} STREQUAL Debug)
add_compile_options(-gdwarf-5 -fsanitize=address -fstack-protector)
add_link_options(-fsanitize=address)
endif()
# Add pmt's CMake module(s)
@@ -29,7 +30,7 @@ include(cmake/generate_headers.cmake)
# Generate header(s)
get_property(FLAGS DIRECTORY PROPERTY COMPILE_OPTIONS)
generateBuildInfo(${FLAGS})
generateBuildInfo("${FLAGS}")
# Add include directories
include_directories(include srclib/libhelper/include srclib/libpartition_map/include)

View File

@@ -29,7 +29,7 @@ add_executable(libhelper_test tests/test.cpp)
# Set linker flags
target_link_libraries(libhelper_test PRIVATE helper_shared)
target_link_options(libhelper_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib")
target_link_options(libhelper_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib" "LINKER:-rpath,/data/local")
target_link_options(helper_shared PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib")
# Set appropriate output names

View File

@@ -33,6 +33,7 @@ enum LogLevels {
};
constexpr mode_t DEFAULT_FILE_PERMS = 0644;
constexpr mode_t DEFAULT_EXTENDED_FILE_PERMS = 0755;
constexpr mode_t DEFAULT_DIR_PERMS = 0755;
constexpr int YES = 1;
constexpr int NO = 0;

View File

@@ -23,6 +23,15 @@
#include <stdarg.h>
#include <libhelper/lib.hpp>
static void __create_log_file(const char* file)
{
int fd = open(file, O_WRONLY | O_TRUNC, DEFAULT_EXTENDED_FILE_PERMS);
if (fd == -1) {
fd = open(file, O_WRONLY | O_CREAT, DEFAULT_EXTENDED_FILE_PERMS);
if (fd != -1) close(fd);
} else if (fd != -1) close(fd);
}
namespace Helper {
Error::Error(const char* format, ...)
@@ -56,7 +65,7 @@ Logger::~Logger()
currentTime().data(),
_oss.str().data());
if (!isExists(_logFile)) createFile(_logFile);
if (!isExists(_logFile)) __create_log_file(_logFile);
FILE* fp = fopen(_logFile, "a");
if (fp != NULL) {

View File

@@ -246,6 +246,7 @@ bool eraseDirectoryRecursive(const std::string_view directory)
std::string readSymlink(const std::string_view entry)
{
LOGN(HELPER, INFO) << __func__ << "(): read symlink request: " << entry << std::endl;
char target[PATH_MAX];
ssize_t len = readlink(entry.data(), target, (sizeof(target) - 1));
if (len == -1) {

View File

@@ -27,20 +27,22 @@ namespace Helper {
std::optional<std::string> sha256Of(const std::string_view path)
{
LOGN(HELPER, INFO) << __func__ << "(): get sha256 of \"" << path << "\" request." << std::endl;
if (!fileIsExists(path)) {
throw Error("Is not exists or not file: %s", path.data());
LOGN(HELPER, INFO) << __func__ << "(): get sha256 of \"" << path << "\" request. Getting full path (if input is link and exists)." << std::endl;
std::string fp = (isLink(path)) ? readSymlink(path) : std::string(path);
if (!fileIsExists(fp)) {
throw Error("Is not exists or not file: %s", fp.data());
return std::nullopt;
}
std::ifstream file(path, std::ios::binary);
std::ifstream file(fp, std::ios::binary);
if (!file) {
throw Error("Cannot open file: %s", path.data());
throw Error("Cannot open file: %s", fp.data());
return std::nullopt;
}
std::vector<unsigned char> hash(picosha2::k_digest_size);
picosha2::hash256(path, hash.begin(), hash.end());
picosha2::hash256(fp, hash.begin(), hash.end());
LOGN(HELPER, INFO) << __func__ << "(): get sha256 of \"" << path << "\" successfull." << std::endl;
return picosha2::bytes_to_hex_string(hash.begin(), hash.end());
}

View File

@@ -123,18 +123,21 @@ std::string currentTime()
std::string runCommandWithOutput(const std::string_view cmd)
{
LOGN(HELPER, INFO) << __func__ << "(): run command and catch out request: " << cmd << std::endl;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.data(), "r"), pclose);
FILE* pipe = popen(cmd.data(), "r");
if (!pipe) {
throw Error("Cannot run command: %s", cmd.data());
return {};
}
std::string end;
std::unique_ptr<FILE, decltype(&pclose)> pipe_holder(pipe, pclose);
std::string output;
char buffer[1024];
while (fgets(buffer, sizeof(buffer), pipe.get()) != nullptr) end += buffer;
while (fgets(buffer, sizeof(buffer), pipe_holder.get()) != nullptr) output += buffer;
return end;
return output;
}
std::string pathJoin(std::string base, std::string relative)

View File

@@ -31,7 +31,7 @@ set_target_properties(partition_map_shared PROPERTIES OUTPUT_NAME "partition_map
set_target_properties(partition_map_static PROPERTIES OUTPUT_NAME "partition_map")
# Set linker flags
target_link_options(libpartition_map_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib")
target_link_options(libpartition_map_test PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib" "LINKER:-rpath,/data/local")
target_link_options(partition_map_shared PRIVATE "LINKER:-rpath,/data/data/com.termux/files/usr/lib")
target_link_libraries(libpartition_map_test PRIVATE partition_map_shared PRIVATE helper_shared)
target_link_libraries(partition_map_shared PRIVATE helper_shared)