Browse Source

Memcheck clean.

Stefano Cossu 4 years ago
parent
commit
ba8ce8b200
4 changed files with 25 additions and 10 deletions
  1. 6 0
      Makefile
  2. 1 0
      src/graph.c
  3. 15 7
      src/store_mdb.c
  4. 3 3
      src/term.c

+ 6 - 0
Makefile

@@ -58,6 +58,7 @@ test_lexer:
 valgrind:
 	valgrind \
 	--leak-check=full --show-leak-kinds=all --track-origins=yes \
+	--log-file=/tmp/lsup_valgrind.log \
 	./bin/test
 
 
@@ -73,6 +74,11 @@ profile: build_parsers
 		-o bin/profile
 
 
+py_test:
+	pip3 install --user . && \
+	python3 test/cpython_test.py
+
+
 # Build a visual dependency graph.
 # Requires cinclude2dot (https://www.flourish.org/cinclude2dot) and Graphviz.
 depgraph: src/* include/*

+ 1 - 0
src/graph.c

@@ -541,6 +541,7 @@ graph_copy_contents (const LSUP_Graph *src, LSUP_Graph *dest)
     LSUP_GraphIterator *add_it = LSUP_graph_add_init (dest);
     while (LSUP_graph_iter_next (it, &spo) != LSUP_END) {
         LSUP_rc add_rc = LSUP_graph_add_iter (add_it, &spo);
+        LSUP_triple_done (&spo);
         if (LIKELY (add_rc == LSUP_OK)) rc = LSUP_OK;
         else if (add_rc < 0) return add_rc;
     }

+ 15 - 7
src/store_mdb.c

@@ -314,6 +314,8 @@ LSUP_mdbstore_new (const char *path, const LSUP_Buffer *default_ctx)
                 LSUP_nsmap_add (nsm, init_nsmap[i][0], init_nsmap[i][1]);
 
             LSUP_mdbstore_nsm_store (store, nsm);
+
+            LSUP_nsmap_free (nsm);
         }
     }
 
@@ -989,10 +991,12 @@ LSUP_mdbstore_nsm_store (LSUP_MDBStore *store, const LSUP_NSMap *nsm)
     for (size_t i = 0; nsm_data[i] != NULL; i++) {
         // At least 1 action. If not OK, it will change during the iteration.
         if (i == 0) rc = LSUP_OK;
+        // On previous error, just clean up the NSM data array.
+        if (rc < 0) goto loop_end;
 
-        pfx_v.mv_data = (void *) nsm_data[i];
+        pfx_v.mv_data = (void *) nsm_data[i][0];
         pfx_v.mv_size = strlen (nsm_data[i][0]) + 1;
-        ns_v.mv_data = (void *) nsm_data[i] + 1;
+        ns_v.mv_data = (void *) nsm_data[i][1];
         ns_v.mv_size = strlen (nsm_data[i][1]) + 1;
 
         // If either ns or pfx exist, skip.
@@ -1002,22 +1006,24 @@ LSUP_mdbstore_nsm_store (LSUP_MDBStore *store, const LSUP_NSMap *nsm)
             mdb_cursor_get (icur, &ns_v, &pfx_v, MDB_SET) != MDB_NOTFOUND
         ) {
             rc = LSUP_CONFLICT;
-            continue;
+            goto loop_end;
         }
 
         db_rc = mdb_cursor_put (dcur, &pfx_v, &ns_v, 0);
         db_rc |= mdb_cursor_put (icur, &ns_v, &pfx_v, 0);
         if (db_rc != MDB_SUCCESS) {
             log_error ("DB error: %s", LSUP_strerror (db_rc));
-            free (nsm_data);
-            return LSUP_DB_ERR;
+            rc = LSUP_DB_ERR;
         }
+loop_end:
+        free (nsm_data[i]);
     }
     free (nsm_data);
 
-    if (UNLIKELY (mdb_txn_commit (txn) != MDB_SUCCESS)) {
+    if (UNLIKELY (rc != LSUP_OK)) mdb_txn_abort (txn);
+    else if (UNLIKELY (mdb_txn_commit (txn) != MDB_SUCCESS)) {
         mdb_txn_abort (txn);
-        return LSUP_TXN_ERR;
+        rc = LSUP_TXN_ERR;
     }
 
     return rc;
@@ -1029,6 +1035,8 @@ LSUP_mdbstore_tkey_exists (LSUP_MDBStore *store, LSUP_Key tkey)
 {
     int db_rc, rc;
     MDB_val key, data;
+    key.mv_data = &tkey;
+    key.mv_size = KLEN;
 
     MDB_txn *txn = NULL;
     mdb_txn_begin (store->env, NULL, MDB_RDONLY, &txn);

+ 3 - 3
src/term.c

@@ -302,9 +302,9 @@ LSUP_triple_done (LSUP_Triple *spo)
 {
     if (UNLIKELY (!spo)) return;
 
-    LSUP_term_done (spo->s);
-    LSUP_term_done (spo->p);
-    LSUP_term_done (spo->o);
+    LSUP_term_free (spo->s);
+    LSUP_term_free (spo->p);
+    LSUP_term_free (spo->o);
 }