Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leaks when using zchunk #322

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ OPTION (ENABLE_PYTHON "Build Python bindings" ON)
OPTION (USE_GPGME "Use GpgMe (instead of rpm library) for OpenPGP key support" ON)
OPTION (USE_RUN_GNUPG_USER_SOCKET "Create a directory for gpg-agent socket in /run/gnugp/user (instead of /run/user)" OFF)
OPTION (ENABLE_SELINUX "Restore SELinux labels on GnuPG directories" ON)
OPTION (WITH_SANITIZERS "Build with address, leak and undefined sanitizers (DEBUG ONLY)" OFF)

INCLUDE (${CMAKE_SOURCE_DIR}/VERSION.cmake)
SET (VERSION "${LIBREPO_MAJOR}.${LIBREPO_MINOR}.${LIBREPO_PATCH}")
Expand Down Expand Up @@ -95,6 +96,12 @@ IF (NOT CURL_FOUND)
MESSAGE(FATAL_ERROR "No CURL library installed")
ENDIF (NOT CURL_FOUND)

IF (WITH_SANITIZERS)
MESSAGE(WARNING "Building with sanitizers enabled!")
ADD_COMPILE_OPTIONS(-fsanitize=address -fsanitize=undefined -fsanitize=leak)
LINK_LIBRARIES(asan ubsan)
ENDIF()

# Generate header files with the configured ABI (e.g. with/without zchunk).

configure_file("librepo/downloadtarget.h.in" "librepo/downloadtarget.h" @ONLY)
Expand Down
9 changes: 9 additions & 0 deletions librepo.spec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# Needs to match how gnupg2 is compiled
%bcond_with run_gnupg_user_socket

%bcond_with sanitizers

%if %{with use_gpgme} && %{with use_selinux}
%global need_selinux 1
%else
Expand Down Expand Up @@ -59,6 +61,12 @@ BuildRequires: pkgconfig(zck) >= 0.9.11
%endif
Requires: libcurl%{?_isa} >= %{libcurl_version}

%if %{with sanitizers}
BuildRequires: libasan
BuildRequires: liblsan
BuildRequires: libubsan
%endif

%description
A library providing C and Python (libcURL like) API to downloading repository
metadata.
Expand Down Expand Up @@ -97,6 +105,7 @@ Python 3 bindings for the librepo library.
-DWITH_ZCHUNK=%{?with_zchunk:ON}%{!?with_zchunk:OFF} \
-DUSE_GPGME=%{?with_use_gpgme:ON}%{!?with_use_gpgme:OFF} \
-DUSE_RUN_GNUPG_USER_SOCKET=%{?with_run_gnupg_user_socket:ON}%{!?with_run_gnupg_user_socket:OFF} \
-DWITH_SANITIZERS=%{?with_sanitizers:ON}%{!?with_sanitizers:OFF} \
%if %{need_selinux}
-DENABLE_SELINUX=ON
%else
Expand Down
7 changes: 3 additions & 4 deletions librepo/downloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,8 @@ prep_zck_header(LrTarget *target, GError **err)
zck = lr_zck_init_read(target->target, target->target->path,
fd, &tmp_err);
if(zck) {
zckCtx *old_zck = zck_dl_get_zck(target->target->zck_dl);
zck_free(&old_zck);
if(!zck_dl_set_zck(target->target->zck_dl, zck)) {
g_set_error(err, LR_DOWNLOADER_ERROR, LRE_ZCK,
"Unable to setup zchunk download context");
Expand Down Expand Up @@ -1228,6 +1230,7 @@ find_local_zck_chunks(LrTarget *target, GError **err)

zckCtx *zck_src = zck_create();
if(!zck_init_read(zck_src, chk_fd)) {
zck_free(&zck_src);
close(chk_fd);
continue;
}
Expand Down Expand Up @@ -1354,8 +1357,6 @@ check_zck(LrTarget *target, GError **err)

if(cks_good == 1) { // All checksums good
g_debug("%s: File is complete", __func__);
if(target->target->zck_dl)
zck_dl_free(&(target->target->zck_dl));
target->zck_state = LR_ZCK_DL_FINISHED;
return TRUE;
}
Expand All @@ -1375,8 +1376,6 @@ check_zck(LrTarget *target, GError **err)
}

if(cks_good == 1) { // All checksums good
if(target->target->zck_dl)
zck_dl_free(&(target->target->zck_dl));
target->zck_state = LR_ZCK_DL_FINISHED;
return TRUE;
}
Expand Down
20 changes: 20 additions & 0 deletions librepo/downloadtarget.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ lr_downloadtarget_free(LrDownloadTarget *target)
if (!target)
return;

#ifdef WITH_ZCHUNK
if (target->zck_dl) {
zckCtx *zck = zck_dl_get_zck(target->zck_dl);
if (zck) {
zck_free(&zck);
}

zckRange *range = zck_dl_get_range(target->zck_dl);
if(range) {
zck_range_free(&range);
}

zck_dl_free(&target->zck_dl);
}
#endif /* WITH_ZCHUNK */

if(target->range) {
free(target->range);
}

g_slist_free_full(target->checksums,
(GDestroyNotify) lr_downloadtargetchecksum_free);
g_string_chunk_free(target->chunk);
Expand Down
Loading