From 49997ef025ee454283722a01210c169ca8910a98 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 9 Jun 2016 21:06:48 +0200 Subject: don't leak an FD in lz4 (de)compression Seen first in #826783, but as this buglog also shows leaked uncompressed files as well we don't close it just yet. (cherry picked from commit 6f35be91c9e86e463bca7df6eadf05412c7b732c) --- apt-pkg/contrib/fileutl.cc | 7 ++++- test/integration/test-apt-helper-cat-file | 16 ++++++++++++ test/interactive-helper/makefile | 6 +++++ test/interactive-helper/test_fileutl.cc | 43 +++++++++++++++++++++++++++++++ test/libapt/file-helpers.cc | 2 +- 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 test/interactive-helper/test_fileutl.cc diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index d880a725d..68298d785 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1535,7 +1535,7 @@ public: return false; unsigned int flags = (Mode & (FileFd::WriteOnly|FileFd::ReadOnly)); - if (backend.OpenDescriptor(iFd, flags) == false) + if (backend.OpenDescriptor(iFd, flags, FileFd::None, true) == false) return false; // Write the file header @@ -1643,6 +1643,11 @@ public: res = LZ4F_freeDecompressionContext(dctx); dctx = nullptr; } + if (backend.IsOpen()) + { + backend.Close(); + filefd->iFd = -1; + } return LZ4F_isError(res) == false; } diff --git a/test/integration/test-apt-helper-cat-file b/test/integration/test-apt-helper-cat-file index f7c94a2b4..3f509189e 100755 --- a/test/integration/test-apt-helper-cat-file +++ b/test/integration/test-apt-helper-cat-file @@ -5,6 +5,15 @@ TESTDIR="$(readlink -f "$(dirname "$0")")" . "$TESTDIR/framework" setupenvironment +TESTTOOL="${BUILDDIRECTORY}/test_fileutl" +msgtest 'Check if we have build the test tool' "$TESTTOOL" +if [ -x "$TESTTOOL" ]; then + msgpass +else + msgskip 'not available' + exit 0 +fi + cat >rootdir/etc/apt/apt.conf.d/rev-as-compressor < +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +static void callsystem(std::string const &call) +{ + auto ret = system(call.c_str()); + if (WIFEXITED(ret) == false || WEXITSTATUS(ret) != 0) + _error->Error("Calling %s failed!", call.c_str()); +} + +int main(int, char ** argv) +{ + auto const pid = getpid(); + std::string ls; + strprintf(ls, "ls -l /proc/%d/fd", pid); + callsystem(ls); + FileFd t; + t.Open(argv[1], FileFd::ReadOnly, FileFd::Extension); + callsystem(ls); + char buf[1024]; + unsigned long long act; + while (t.Read(buf, sizeof(buf), &act)) + if (act == 0) + break; + callsystem(ls); + t.Seek(5); + callsystem(ls); + t.Close(); + callsystem(ls); + auto const ret = _error->PendingError(); + _error->DumpErrors(); + return ret; +} diff --git a/test/libapt/file-helpers.cc b/test/libapt/file-helpers.cc index 6811c4158..48d8a9fbb 100644 --- a/test/libapt/file-helpers.cc +++ b/test/libapt/file-helpers.cc @@ -74,7 +74,7 @@ void helperCreateTemporaryFile(std::string const &id, FileFd &fd, std::string * unlink(tempfile); free(tempfile); - EXPECT_TRUE(fd.OpenDescriptor(tempfile_fd, FileFd::ReadWrite)); + EXPECT_TRUE(fd.OpenDescriptor(tempfile_fd, FileFd::ReadWrite, true)); if (content != NULL) { ASSERT_TRUE(fd.Write(content, strlen(content))); -- cgit v1.2.3