From bd707acfee7385be846e9851e305e47cea1a32ac Mon Sep 17 00:00:00 2001 From: Juan RP Date: Tue, 31 Mar 2020 18:36:04 +0200 Subject: [PATCH] repo: fix a double free with invalid repodata. Fixes #248 Added new test case to verify. --- lib/repo.c | 5 ++-- tests/xbps/libxbps/shell/Kyuafile | 1 + tests/xbps/libxbps/shell/Makefile | 2 +- tests/xbps/libxbps/shell/conf_files_test.sh | 0 tests/xbps/libxbps/shell/repo_test.sh | 27 +++++++++++++++++++++ 5 files changed, 31 insertions(+), 4 deletions(-) mode change 100755 => 100644 tests/xbps/libxbps/shell/conf_files_test.sh create mode 100644 tests/xbps/libxbps/shell/repo_test.sh diff --git a/lib/repo.c b/lib/repo.c index 91fcfb7a..f3f6ed4f 100644 --- a/lib/repo.c +++ b/lib/repo.c @@ -156,7 +156,6 @@ repo_open_local(struct xbps_repo *repo, const char *repofile) xbps_dbg_printf(repo->xhp, "[repo] `%s' failed to open repodata archive %s\n", repofile, strerror(rv)); - xbps_repo_close(repo); return false; } if ((repo->idx = repo_get_dict(repo)) == NULL) { @@ -164,7 +163,6 @@ repo_open_local(struct xbps_repo *repo, const char *repofile) " index on archive, removing file.\n", repofile); /* broken archive, remove it */ (void)unlink(repofile); - xbps_repo_close(repo); return false; } xbps_dictionary_make_immutable(repo->idx); @@ -368,7 +366,8 @@ xbps_repo_open(struct xbps_handle *xhp, const char *url) void xbps_repo_close(struct xbps_repo *repo) { - assert(repo); + if (!repo) + return; if (repo->ar != NULL) archive_read_finish(repo->ar); diff --git a/tests/xbps/libxbps/shell/Kyuafile b/tests/xbps/libxbps/shell/Kyuafile index d51f3dff..e27f783e 100644 --- a/tests/xbps/libxbps/shell/Kyuafile +++ b/tests/xbps/libxbps/shell/Kyuafile @@ -29,3 +29,4 @@ atf_test_program{name="preserve_test"} atf_test_program{name="orphans_test"} atf_test_program{name="noextract_files_test"} atf_test_program{name="transaction_check_revdeps_test"} +atf_test_program{name="repo_test"} diff --git a/tests/xbps/libxbps/shell/Makefile b/tests/xbps/libxbps/shell/Makefile index a8d46d5a..22c52647 100644 --- a/tests/xbps/libxbps/shell/Makefile +++ b/tests/xbps/libxbps/shell/Makefile @@ -8,7 +8,7 @@ TESTSHELL+= issue31_test scripts_test incorrect_deps_test TESTSHELL+= vpkg_test install_test preserve_files_test configure_test TESTSHELL+= update_shlibs_test update_hold_test update_repolock_test TESTSHELL+= cyclic_deps_test conflicts_test update_itself_test -TESTSHELL+= hold_test ignore_test preserve_test +TESTSHELL+= hold_test ignore_test preserve_test repo_test TESTSHELL+= noextract_files_test orphans_test transaction_check_revdeps_test EXTRA_FILES = Kyuafile diff --git a/tests/xbps/libxbps/shell/conf_files_test.sh b/tests/xbps/libxbps/shell/conf_files_test.sh old mode 100755 new mode 100644 diff --git a/tests/xbps/libxbps/shell/repo_test.sh b/tests/xbps/libxbps/shell/repo_test.sh new file mode 100644 index 00000000..30022acf --- /dev/null +++ b/tests/xbps/libxbps/shell/repo_test.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env atf-sh + +atf_test_case repo_close + +repo_close_head() { + atf_set "descr" "Tests for pkg repos: truncate repo size to 0" +} + +repo_close_body() { + mkdir -p repo pkg_A + cd repo + xbps-create -A noarch -n A-1.0_1 -s "A pkg" ../pkg_A + atf_check_equal $? 0 + xbps-rindex -d -a $PWD/*.xbps + atf_check_equal $? 0 + cd .. + xbps-install -C empty.conf -r root --repository=repo -yn A + atf_check_equal $? 0 + truncate --size 0 repo/*-repodata + xbps-install -C empty.conf -r root --repository=repo -yn A + # ENOENT because invalid repodata + atf_check_equal $? 2 +} + +atf_init_test_cases() { + atf_add_test_case repo_close +}