Skip to content

Commit

Permalink
Fixed potential crash on Windows by passing a pointer to a DWORD inst…
Browse files Browse the repository at this point in the history
…ead of nullpointer

According to the documentation, that parameter can only be NULL for OVERLAPPED io, which is not what we are doing here.
  • Loading branch information
FlorianReimold committed Nov 7, 2023
1 parent c34e3eb commit 021deb4
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion fineftp-server/src/win32/file_man.cpp
Expand Up @@ -129,6 +129,7 @@ WriteableFile::WriteableFile(const std::string& filename, std::ios::openmode mod
dwCreationDisposition = CREATE_ALWAYS; // Not Append => Create new file
}

// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
#if !defined(__GNUG__)
auto wfilename = StrConvert::Utf8ToWide(filename);
handle_ = ::CreateFileW(wfilename.c_str(), dwDesiredAccess, FILE_SHARE_DELETE, nullptr, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, nullptr);
Expand Down Expand Up @@ -161,7 +162,8 @@ void WriteableFile::close()

void WriteableFile::write(const char* data, std::size_t sz)
{
(void)::WriteFile(handle_, data, static_cast<DWORD>(sz), nullptr, nullptr);
DWORD bytes_written{}; // Unused, but required according to https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile
(void)::WriteFile(handle_, data, static_cast<DWORD>(sz), &bytes_written, nullptr);
}

}
Expand Down

2 comments on commit 021deb4

@huawuxin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This crash is 100% reproducible under 32-bit Windows XP. Verified fixed now. Thanks.

@FlorianReimold
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This crash is 100% reproducible under 32-bit Windows XP. Verified fixed now. Thanks.

Thanks for the feedback! I will release a new version, soon!

Please sign in to comment.