From 88bf9a260c8b7c9b814d436a0e9cc7047ca33f75 Mon Sep 17 00:00:00 2001 From: astrelsky Date: Tue, 5 Feb 2019 18:36:44 -0500 Subject: [PATCH 1/2] Fixed FilesystemPath::isFile return value. Resolves #490 --- CHANGELOG.md | 1 + src/utils/filesystem_path.cpp | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 050386e14..f975102f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ # dev +* Fix: FilesystemPath::isFile ([#490] (https://github.com/avast-tl/retdec/issues/490)). * New Feature: Added presentation of imported types and TypeRef hashes for .NET binaries ([#363](https://github.com/avast-tl/retdec/issues/363), [#364](https://github.com/avast-tl/retdec/issues/364), [#428](https://github.com/avast-tl/retdec/issues/428)). * New Feature: Added computation and presentation of icon hashes for exact and also similarity matching in PE files ([#339](https://github.com/avast-tl/retdec/issues/339)). * Enhancement: Added support for build and run on FreeBSD and potentially on other BSD OSes ([#476](https://github.com/avast-tl/retdec/pull/476)). diff --git a/src/utils/filesystem_path.cpp b/src/utils/filesystem_path.cpp index 3b0410498..d15ad7a19 100644 --- a/src/utils/filesystem_path.cpp +++ b/src/utils/filesystem_path.cpp @@ -141,7 +141,11 @@ class FilesystemPathImplWindows : public FilesystemPathImpl virtual bool isFile() override { - return !isDirectory(); + WIN32_FIND_DATA ffd; + if (FindFirstFile(_path.c_str(), &ffd) == reinterpret_cast(-1)) + return false; + + return !(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); } virtual bool isDirectory() override @@ -231,7 +235,11 @@ class FilesystemPathImplUnix : public FilesystemPathImpl virtual bool isFile() override { - return !isDirectory(); + struct stat st; + if (stat(_path.c_str(), &st) != 0) + return false; + + return S_ISREG(st.st_mode); } virtual bool isDirectory() override From 42aecd13996dfa3ce2d8d51f1a4c8359a3e012df Mon Sep 17 00:00:00 2001 From: astrelsky Date: Wed, 6 Feb 2019 09:59:43 -0500 Subject: [PATCH 2/2] Refactoring of FilesystemPathImplWindows Instances of reinterpret_cast(-1) in FilesystemPathImplWindows have been replaced with INVALID_HANDLE_VALUE for readability. GetFileAttributes was utilized in place of FindFirstFile to check for file existance and file attributes. --- src/utils/filesystem_path.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/utils/filesystem_path.cpp b/src/utils/filesystem_path.cpp index d15ad7a19..aed9fa787 100644 --- a/src/utils/filesystem_path.cpp +++ b/src/utils/filesystem_path.cpp @@ -114,7 +114,7 @@ class FilesystemPathImplWindows : public FilesystemPathImpl subpaths.clear(); HANDLE hFnd = FindFirstFile(examineDir.c_str(), &ffd); - if (hFnd == reinterpret_cast(-1)) + if (hFnd == INVALID_HANDLE_VALUE) return false; do @@ -141,20 +141,16 @@ class FilesystemPathImplWindows : public FilesystemPathImpl virtual bool isFile() override { - WIN32_FIND_DATA ffd; - if (FindFirstFile(_path.c_str(), &ffd) == reinterpret_cast(-1)) - return false; - - return !(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); + DWORD attributes = GetFileAttributes(_path.c_str()); + return (attributes != INVALID_FILE_ATTRIBUTES) && + (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0; } virtual bool isDirectory() override { - WIN32_FIND_DATA ffd; - if (FindFirstFile(_path.c_str(), &ffd) == reinterpret_cast(-1)) - return false; - - return ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; + DWORD attributes = GetFileAttributes(_path.c_str()); + return (attributes != INVALID_FILE_ATTRIBUTES) && + (attributes & FILE_ATTRIBUTE_DIRECTORY); } virtual bool isAbsolute() override