Forráskód Böngészése

Fix memory leaks.

scossu 9 hónapja
szülő
commit
4b203e5de1
3 módosított fájl, 25 hozzáadás és 26 törlés
  1. 14 5
      src/buffer.c
  2. 10 21
      src/graph.c
  3. 1 0
      test/test_graph.c

+ 14 - 5
src/buffer.c

@@ -16,14 +16,23 @@ LSUP_rc
 LSUP_buffer_init (
         LSUP_Buffer *buf, const size_t size, const unsigned char *data)
 {
-    // If size is zero, addr becomes NULL.
-    unsigned char *tmp = realloc (buf->addr, size);
-    if (UNLIKELY (size > 0 && tmp == NULL)) return LSUP_MEM_ERR;
+    // Do not rely on glibc realloc to handle zero size. See man 3 realloc -
+    // Nonportable behavior.
+    if (size == 0) {
+        if (buf->addr) {
+            free (buf->addr);
+            buf->addr = NULL;
+        }
+    } else {
+        // If buf->addr is NULL, realloc == malloc. This is portable.
+        unsigned char *tmp = realloc (buf->addr, size);
+        if (UNLIKELY (size > 0 && tmp == NULL)) return LSUP_MEM_ERR;
 
-    buf->addr = tmp;
+        buf->addr = tmp;
+    }
     buf->size = size;
 
-    if (data) memcpy (buf->addr, data, buf->size);
+    if (data && buf->addr) memcpy (buf->addr, data, buf->size);
 
     return LSUP_OK;
 }

+ 10 - 21
src/graph.c

@@ -29,9 +29,6 @@ struct graph_iter_t {
 inline static LSUP_rc
 graph_iter_next_buffer (LSUP_GraphIterator *it);
 
-inline static LSUP_rc
-graph_iter_alloc_buffers (LSUP_GraphIterator *it);
-
 
 #define ENTRY(a, b) (be) == (LSUP_STORE_##a) ||
 static inline bool
@@ -456,7 +453,15 @@ LSUP_graph_lookup_txn (
     }
 
     it->graph = gr;
-    RCNL (graph_iter_alloc_buffers (it));
+
+    if (it->graph->store->sif->features & LSUP_STORE_COW) {
+        // Copy-on-wite store.
+        it->sspo = BTRP_DUMMY;
+
+        if (UNLIKELY (it->sspo == NULL)) return NULL;
+    } else {
+        // TODO copy-on-retrieval store. No implementations yet.
+    }
 
     return it;
 }
@@ -497,6 +502,7 @@ LSUP_graph_iter_free (LSUP_GraphIterator *it)
      */
     if (it->graph->store->sif->features & LSUP_STORE_COW) {
         LSUP_btriple_free_shallow (it->sspo);
+        log_debug ("Freeing dummy triple @ %p", it->sspo);
     } else {
         // TODO copy-on-retrieval stores. None yet.
     }
@@ -715,23 +721,6 @@ graph_iter_next_buffer (LSUP_GraphIterator *it)
 { return it->graph->store->sif->lu_next_fn (it->data, it->sspo, NULL); }
 
 
-/** @brief Properly allocate temporary byte buffers in advance of iteration.
- */
-inline LSUP_rc
-graph_iter_alloc_buffers (LSUP_GraphIterator *it)
-{
-    if (it->graph->store->sif->features & LSUP_STORE_COW) {
-        // Copy-on-wite store.
-        it->sspo = BTRP_DUMMY;
-        if (it->sspo == NULL) return LSUP_MEM_ERR;
-    } else {
-        // TODO copy-on-retrieval store. No implementations yet.
-    }
-
-    return LSUP_OK;
-}
-
-
 /**
  * Extern inline definitions.
  */

+ 1 - 0
test/test_graph.c

@@ -134,6 +134,7 @@ _graph_get (LSUP_StoreType type)
     LSUP_graph_free (gr3);
     LSUP_graph_free (gr4);
     free_triples (trp);
+    if (type != LSUP_STORE_HTABLE) LSUP_store_free (store);
 
     return 0;
 }