From 8cbc874ef01f3d161d4ca253167d8f80e689c814 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 29 Jan 2023 20:16:37 +0100
Subject: Do not allocate memory for file mmap()'ed contents.

---
 src/analysis/contents/file.c       | 20 ++++++++++++++------
 src/analysis/contents/memory-int.h |  1 +
 src/analysis/contents/memory.c     | 12 ++++++++++--
 3 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/src/analysis/contents/file.c b/src/analysis/contents/file.c
index 4f102d8..7497667 100644
--- a/src/analysis/contents/file.c
+++ b/src/analysis/contents/file.c
@@ -49,6 +49,7 @@ struct _GFileContent
     GMemoryContent parent;                  /* A laisser en premier        */
 
     char *filename;                         /* Fichier chargé en mémoire   */
+    int fd;                                 /* Flux ouvert en lectureu     */
 
 };
 
@@ -136,6 +137,7 @@ static void g_file_content_class_init(GFileContentClass *klass)
 static void g_file_content_init(GFileContent *content)
 {
     content->filename = NULL;
+    content->fd = -1;
 
 }
 
@@ -193,8 +195,19 @@ static void g_file_content_dispose(GFileContent *content)
 
 static void g_file_content_finalize(GFileContent *content)
 {
+    GMemoryContent *base;                   /* Structure parente           */
+
     free(content->filename);
 
+    if (content->fd != -1)
+    {
+        base = G_MEMORY_CONTENT(content);
+        munmap(base->data, base->length);
+
+        close(content->fd);
+
+    }
+
     G_OBJECT_CLASS(g_file_content_parent_class)->finalize(G_OBJECT(content));
 
 }
@@ -254,14 +267,9 @@ GBinContent *g_file_content_new(const char *filename)
 
     base = G_MEMORY_CONTENT(result);
 
-    base->data = malloc(info.st_size);
-    memcpy(base->data, content, info.st_size);
-
+    base->data = content;
     base->length = info.st_size;
 
-    munmap(content, info.st_size);
-    close(fd);
-
     return G_BIN_CONTENT(result);
 
  file_error:
diff --git a/src/analysis/contents/memory-int.h b/src/analysis/contents/memory-int.h
index 621e4a6..749e984 100644
--- a/src/analysis/contents/memory-int.h
+++ b/src/analysis/contents/memory-int.h
@@ -38,6 +38,7 @@ struct _GMemoryContent
 
     bin_t *data;                            /* Contenu binaire représenté  */
     phys_t length;                          /* Taille totale du contenu    */
+    bool allocated;                         /* Nature de la zone de données*/
 
     char *full_desc;                        /* Description de l'ensemble   */
     char *desc;                             /* Description de l'ensemble   */
diff --git a/src/analysis/contents/memory.c b/src/analysis/contents/memory.c
index 9ddc4fa..f80a01a 100644
--- a/src/analysis/contents/memory.c
+++ b/src/analysis/contents/memory.c
@@ -196,6 +196,7 @@ static void g_memory_content_init(GMemoryContent *content)
 
     content->data = NULL;
     content->length = 0;
+    content->allocated = false;
 
     content->full_desc = strdup("In-memory content");
     content->desc = strdup("In-memory content");
@@ -302,8 +303,11 @@ static void g_memory_content_dispose(GMemoryContent *content)
 
 static void g_memory_content_finalize(GMemoryContent *content)
 {
-    if (content->data != NULL)
-        free(content->data);
+    if (content->allocated)
+    {
+        if (content->data != NULL)
+            free(content->data);
+    }
 
     if (content->desc != NULL)
         free(content->desc);
@@ -347,6 +351,7 @@ GBinContent *g_memory_content_new(const bin_t *data, phys_t size)
 
     result->data = allocated;
     result->length = size;
+    result->allocated = true;
 
     return G_BIN_CONTENT(result);
 
@@ -929,6 +934,9 @@ static bool g_memory_content_load(GMemoryContent *content, GObjectStorage *stora
     {
         content->data = malloc(length);
         result = (content->data != NULL);
+
+        content->allocated = true;
+
     }
 
     if (result)
-- 
cgit v0.11.2-87-g4458