Browse Source

Do not copy passed graph URI in LSUP_graph_new().

scossu 1 year ago
parent
commit
3f1569f63e
2 changed files with 16 additions and 21 deletions
  1. 10 17
      src/desc.c
  2. 6 4
      test/test_desc.c

+ 10 - 17
src/desc.c

@@ -11,8 +11,7 @@ LSR_desc_new_multi (LSUP_Graph *const *data, LSR_Desc **rsrc_p)
     uuid_generate_random (rsrc->id);
 
     // Default context graph.
-    rsrc->main_data = LSUP_graph_new (
-            NULL, LSUP_term_copy (LSUP_default_ctx), NULL);
+    rsrc->main_data = LSUP_graph_new (NULL, LSUP_default_ctx, NULL);
     log_debug (
             "Main data graph: %s",
             LSUP_graph_uri(rsrc->main_data)->data);
@@ -51,8 +50,7 @@ LSR_desc_new_multi (LSUP_Graph *const *data, LSR_Desc **rsrc_p)
         LSUP_term_free (rel_uri);
         if (rc < 0) goto finally;
 
-        rsrc->user_data[i] = LSUP_graph_new (
-                NULL, LSUP_term_copy (gr_uri), NULL);
+        rsrc->user_data[i] = LSUP_graph_new (NULL, gr_uri, NULL);
         log_debug (
                 "User data graph (@%p): %s",
                 LSUP_graph_uri(rsrc->user_data[i]),
@@ -231,6 +229,7 @@ loop_end:
     LSUP_triple_init (dest_spo, dest_s, dest_p, dest_o);
     LSUP_graph_add_iter (admin_add_it, dest_spo);
     LSUP_term_free (dest_p);
+    LSUP_term_free (dest_s); // === gr_uri
 
     LSUP_graph_add_done (admin_add_it);
     admin_add_it = NULL;
@@ -301,8 +300,7 @@ LSR_desc_store (const LSR_Desc *rsrc)
     LSUP_rc rc = LSUP_NOACTION;
     for (size_t i = 0; rsrc->user_data[i] != NULL; i++) {
         tmp_gr = LSUP_graph_new (
-                LSR_store,
-                LSUP_term_copy (LSUP_graph_uri (rsrc->user_data[i])), NULL);
+                LSR_store, LSUP_graph_uri (rsrc->user_data[i]), NULL);
         if (UNLIKELY (!tmp_gr)) {
             rc = LSUP_MEM_ERR;
             goto fail;
@@ -314,8 +312,7 @@ LSR_desc_store (const LSR_Desc *rsrc)
 
     // Update admin data.
     tmp_gr = LSUP_graph_new (
-                LSR_store,
-                LSUP_term_copy (LSUP_graph_uri (rsrc->admin_data)), NULL);
+                LSR_store, LSUP_graph_uri (rsrc->admin_data), NULL);
     if (UNLIKELY (!tmp_gr)) {
         rc = LSUP_MEM_ERR;
         goto fail;
@@ -326,8 +323,7 @@ LSR_desc_store (const LSR_Desc *rsrc)
 
     // Update graph metadata.
     tmp_gr = LSUP_graph_new (
-                LSR_store,
-                LSUP_term_copy (LSUP_graph_uri (rsrc->main_data)), NULL);
+                LSR_store, LSUP_graph_uri (rsrc->main_data), NULL);
     if (UNLIKELY (!tmp_gr)) {
         rc = LSUP_MEM_ERR;
         goto fail;
@@ -362,8 +358,7 @@ LSR_desc_get (const uuid_t id, LSR_Desc **rsrc_p)
 {
     LSUP_rc rc = LSUP_OK;
 
-    LSUP_Graph *main_gr = LSUP_graph_new (
-            LSR_store, LSUP_term_copy (LSUP_default_ctx), NULL);
+    LSUP_Graph *main_gr = LSUP_graph_new (LSR_store, LSUP_default_ctx, NULL);
     if (!main_gr) return LSUP_DB_ERR;
 
     LSUP_Triple *spo = LSUP_triple_new (
@@ -394,7 +389,7 @@ LSR_desc_get (const uuid_t id, LSR_Desc **rsrc_p)
             main_gr, NULL, spo->p, spo->s, &ct);
     LSUP_Graph **data = calloc (sizeof (*data), ct + 1);
     while (LSUP_graph_iter_next (it, &spo)) {
-        data[i] = LSUP_graph_new (LSR_store, LSUP_term_copy (spo->s), NULL);
+        data[i] = LSUP_graph_new (LSR_store, spo->s, NULL);
         if (! data[i++]) break; // Last slot remains NULL (sentinel).
     }
     LSUP_graph_iter_free (it);
@@ -417,8 +412,7 @@ LSUP_Graph *
 LSR_desc_metadata (const LSR_Desc *rsrc)
 {
     LSUP_Graph *res = LSUP_graph_new (
-            LSR_store, LSUP_term_copy (LSUP_graph_uri (rsrc->admin_data)),
-            NULL);
+            LSR_store, LSUP_graph_uri (rsrc->admin_data), NULL);
     LSUP_graph_copy_contents (rsrc->admin_data, res);
 
     return res;
@@ -435,8 +429,7 @@ LSR_desc_user_data (const LSR_Desc *rsrc)
 
     for (size_t i = 0; i < ct; i++) {
         res[i] = LSUP_graph_new (
-                LSR_store,
-                LSUP_term_copy (LSUP_graph_uri (rsrc->user_data[i])), NULL);
+                LSR_store, LSUP_graph_uri (rsrc->user_data[i]), NULL);
         LSUP_graph_copy_contents (rsrc->user_data[i], res[i]);
     }
 

+ 6 - 4
test/test_desc.c

@@ -36,13 +36,15 @@ test_desc_create()
         {NULL}
     };
 
-    LSUP_Graph *gr1 = LSUP_graph_new (
-            NULL, LSUP_iriref_new ("#usr1", NULL), NULL);
+    LSUP_Term *usr1_uri = LSUP_iriref_new ("#usr1", NULL);
+    LSUP_Graph *gr1 = LSUP_graph_new (NULL, usr1_uri, NULL);
+    LSUP_term_free (usr1_uri);
     ASSERT (gr1, "Error creating graph!");
     EXPECT_PASS (LSUP_graph_add (gr1, trp1, NULL));
 
-    LSUP_Graph *gr2 = LSUP_graph_new (
-            NULL, LSUP_iriref_new ("#usr2", NULL), NULL);
+    LSUP_Term *usr2_uri = LSUP_iriref_new ("#usr2", NULL);
+    LSUP_Graph *gr2 = LSUP_graph_new (NULL, usr2_uri, NULL);
+    LSUP_term_free (usr2_uri);
     ASSERT (gr2, "Error creating graph!");
     EXPECT_PASS (LSUP_graph_add (gr2, trp2, NULL));