From 1d20cd6a17f73ac6957ea2a53f6151eb6e77b380 Mon Sep 17 00:00:00 2001 From: Squareys Date: Sat, 15 Aug 2020 12:04:42 +0200 Subject: [PATCH] Directory: Close a leaking handle in list() and test Signed-off-by: Squareys --- src/Corrade/Utility/Directory.cpp | 1 + src/Corrade/Utility/Test/DirectoryTest.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Corrade/Utility/Directory.cpp b/src/Corrade/Utility/Directory.cpp index 98ac02dc3..5da47fee2 100644 --- a/src/Corrade/Utility/Directory.cpp +++ b/src/Corrade/Utility/Directory.cpp @@ -628,6 +628,7 @@ std::vector list(const std::string& path, Flags flags) { WIN32_FIND_DATAW data; HANDLE hFile = FindFirstFileW(widen(join(path, "*")).data(), &data); if(hFile == INVALID_HANDLE_VALUE) return list; + Containers::ScopeGuard exit{hFile, FindClose}; /* Explicitly add `.` for compatibility with other systems */ if(!(flags & (Flag::SkipDotAndDotDot|Flag::SkipDirectories))) list.push_back("."); diff --git a/src/Corrade/Utility/Test/DirectoryTest.cpp b/src/Corrade/Utility/Test/DirectoryTest.cpp index 8a37e0306..9067d82da 100644 --- a/src/Corrade/Utility/Test/DirectoryTest.cpp +++ b/src/Corrade/Utility/Test/DirectoryTest.cpp @@ -40,6 +40,10 @@ #include +#ifdef CORRADE_TARGET_WINDOWS +#include +#endif + #include "configure.h" #ifdef CORRADE_UTILITY_LINUX @@ -933,9 +937,22 @@ void DirectoryTest::list() { "CTest is not able to run XCTest executables properly in the simulator."); #endif + #ifdef CORRADE_TARGET_WINDOWS + DWORD handleCount = 0; + GetProcessHandleCount(GetCurrentProcess(), &handleCount); + #endif + CORRADE_COMPARE_AS(Directory::list(_testDir), (std::vector{".", "..", "dir", "file"}), TestSuite::Compare::SortedContainer); + + #ifdef CORRADE_TARGET_WINDOWS + /* Ensure we are not leaking handles */ + DWORD newHandleCount = 0; + GetProcessHandleCount(GetCurrentProcess(), &newHandleCount); + + CORRADE_COMPARE(newHandleCount, handleCount); + #endif } void DirectoryTest::listSkipDirectories() {