Skip to content

Commit

Permalink
Don't keep file handle open
Browse files Browse the repository at this point in the history
  • Loading branch information
benwoo1110 committed Sep 23, 2022
1 parent cf2750d commit fdc3c95
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
13 changes: 8 additions & 5 deletions MalwareToolbox.LibraryC/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,33 @@ bool WinFile::Load()
}

const DWORD file_size = GetFileSize(file, nullptr);
const LPVOID file_data_src = HeapAlloc(GetProcessHeap(), 0, file_size);
const LPVOID file_data = HeapAlloc(GetProcessHeap(), 0, file_size);

DWORD bytes_read;
const bool read_status = ReadFile(file, file_data, file_size, &bytes_read, nullptr);
const bool read_status = ReadFile(file, file_data_src, file_size, &bytes_read, nullptr);
if (!read_status)
{
std::cout << "Failed to read file: " << GetLastError() << std::endl;
return false;
}
m_file = file;

CopyMemory(file_data, file_data_src, file_size);

m_file_size = file_size;
m_file_data = file_data;
m_file_loaded = true;

CloseHandle(file);
HeapFree(GetProcessHeap(), 0, file_data_src);

return true;
}

bool WinFile::Unload()
{
if (m_file_lock) return false;

if (m_file) CloseHandle(m_file);
if (m_file_data) HeapFree(GetProcessHeap(), 0, m_file_data);
m_file = nullptr;
m_file_data = nullptr;
m_file_size = NULL;
m_file_loaded = false;
Expand Down
1 change: 0 additions & 1 deletion MalwareToolbox.LibraryC/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ namespace MalwareToolbox {

protected:
String^ m_file_path;
HANDLE m_file;
DWORD m_file_size;
LPVOID m_file_data;
bool m_file_loaded;
Expand Down

0 comments on commit fdc3c95

Please sign in to comment.