Skip to content

Commit

Permalink
Fixed FilesystemPath::isFile return value. (#491)
Browse files Browse the repository at this point in the history
* Fixed FilesystemPath::isFile return value.

Resolves #490

* Refactoring of FilesystemPathImplWindows

Instances of reinterpret_cast<HANDLE>(-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.
  • Loading branch information
Andrew Strelsky authored and metthal committed Feb 10, 2019
1 parent 5fb7c9f commit a6ba252
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: Reduced the needed stack space in `retdec-llvmir2hll` ([#492](https://github.com/avast-tl/retdec/pull/492)).
Expand Down
20 changes: 12 additions & 8 deletions src/utils/filesystem_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class FilesystemPathImplWindows : public FilesystemPathImpl

subpaths.clear();
HANDLE hFnd = FindFirstFile(examineDir.c_str(), &ffd);
if (hFnd == reinterpret_cast<HANDLE>(-1))
if (hFnd == INVALID_HANDLE_VALUE)
return false;

do
Expand All @@ -141,16 +141,16 @@ class FilesystemPathImplWindows : public FilesystemPathImpl

virtual bool isFile() override
{
return !isDirectory();
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<HANDLE>(-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
Expand Down Expand Up @@ -231,7 +231,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
Expand Down

0 comments on commit a6ba252

Please sign in to comment.