foxygit / doom Log in
commit 6d39b0302d38e83185ece000bc4237cd798740ca
Author:     David Carlier <devnexen@gmail.com>
AuthorDate: Mon Apr 1 19:42:23 2019 +0100
Commit:     David Carlier <devnexen@gmail.com>
CommitDate: Sat Jun 8 06:59:30 2019 +0100

    Checking possible wrong mapping in memory and other little tweaks.
---
 src/w_file_posix.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/w_file_posix.c b/src/w_file_posix.c
index 9f74b5df..d9835f4d 100644
--- a/src/w_file_posix.c
+++ b/src/w_file_posix.c
@@ -60,13 +60,15 @@ static void MapFile(posix_wad_file_t *wad, const char *filename)
                   protection, flags,
                   wad->handle, 0);

-    wad->wad.mapped = result;
-
-    if (result == NULL)
+    if (result == NULL || result == (void *)-1)
     {
         fprintf(stderr, "W_POSIX_OpenFile: Unable to mmap() %s - %s\n",
                         filename, strerror(errno));
     }
+    else
+    {
+        wad->wad.mapped = result;
+    }
 }

 unsigned int GetFileLength(int handle)
@@ -92,6 +94,7 @@ static wad_file_t *W_POSIX_OpenFile(const char *path)
     result->wad.file_class = &posix_wad_file;
     result->wad.length = GetFileLength(handle);
     result->wad.path = M_StringDuplicate(path);
+    result->wad.mapped = NULL;
     result->handle = handle;

     // Try to map the file into memory with mmap:
@@ -110,7 +113,11 @@ static void W_POSIX_CloseFile(wad_file_t *wad)
     // If mapped, unmap it.

     // Close the file
-
+
+    if (posix_wad->wad.mapped)
+    {
+        munmap(posix_wad->wad.mapped, posix_wad->wad.length);
+    }
     close(posix_wad->handle);
     Z_Free(posix_wad);
 }