GothicKit / ZenKit

A re-implementation of file formats used by the early 2000's ZenGin
http://zk.gothickit.dev/
MIT License
44 stars 10 forks source link

[v1.0.0] Mmap fails on empty files #24

Closed lmichaelis closed 1 year ago

lmichaelis commented 1 year ago
    Hit runtime exception, at loading "Chronicles of Myrtana" mod:

...\TheChroniclesOfMyrtana\_work\Data\Scripts\_compiled\Fight.dat - is empty file, that causes mmap to fail internally.

// _deps\mio-src\include\mio\detail\mmap.ipp
inline mmap_context memory_map(const file_handle_type file_handle, const int64_t offset,
    const int64_t length, const access_mode mode, std::error_code& error)
{
...
    char* mapping_start = static_cast<char*>(::MapViewOfFile(
            file_mapping_handle,
            mode == access_mode::read ? FILE_MAP_READ : FILE_MAP_WRITE,
            win::int64_high(aligned_offset),
            win::int64_low(aligned_offset),
            length_to_map));
    if(mapping_start == nullptr) // <-- file exists, but mapping is null
    {
...

Originally posted by @Try in https://github.com/Try/OpenGothic/issues/271#issuecomment-1301469337

lmichaelis commented 1 year ago

This seems to be a problem with MapViewOfFile rather than mio. If it returns nullptr if the file is empty, then mio can't do anything about that. Regardless of that, any read from that file would fail anyways so the caller has to deal with the error.