preserve invariants while calling GCP_detatch_path while reorging paths
authorChristian Grothoff <christian@grothoff.org>
Wed, 1 Feb 2017 19:49:52 +0000 (20:49 +0100)
committerChristian Grothoff <christian@grothoff.org>
Wed, 1 Feb 2017 19:49:52 +0000 (20:49 +0100)
src/cadet/gnunet-service-cadet-new_paths.c
src/cadet/gnunet-service-cadet-new_peer.c

index e2d8da6879fc4098c8d1a0fb436d381759010178..14e7b75d56ed2f75d8b53a9a4a314408076f805b 100644 (file)
@@ -233,6 +233,7 @@ GCPP_release (struct CadetPeerPath *path)
        GCPP_2s (path));
   path->hn = NULL;
   entry = path->entries[path->entries_length - 1];
+  GNUNET_assert (path == entry->path);
   while (1)
   {
     /* cut 'off' end of path */
@@ -247,6 +248,7 @@ GCPP_release (struct CadetPeerPath *path)
 
     /* see if new peer at the end likes this path any better */
     entry = path->entries[path->entries_length - 1];
+    GNUNET_assert (path == entry->path);
     path->hn = GCP_attach_path (entry->peer,
                                 path,
                                 path->entries_length - 1,
@@ -386,7 +388,6 @@ extend_path (struct CadetPeerPath *path,
              int force)
 {
   unsigned int old_len = path->entries_length;
-  struct GNUNET_CONTAINER_HeapNode *hn;
   int i;
 
   /* Expand path */
@@ -412,18 +413,21 @@ extend_path (struct CadetPeerPath *path,
 
   /* If we extend an existing path, detach it from the
      old owner and re-attach to the new one */
-  hn = NULL;
+  GCP_detach_path (path->entries[old_len-1]->peer,
+                   path,
+                   path->hn);
+  path->hn = NULL;
   for (i=num_peers-1;i>=0;i--)
   {
     struct CadetPeerPathEntry *entry = path->entries[old_len + i];
 
     path->entries_length = old_len + i + 1;
     recalculate_path_desirability (path);
-    hn = GCP_attach_path (peers[i],
-                          path,
-                          old_len + (unsigned int) i,
-                          GNUNET_YES);
-    if (NULL != hn)
+    path->hn = GCP_attach_path (peers[i],
+                                path,
+                                old_len + (unsigned int) i,
+                                GNUNET_NO);
+    if (NULL != path->hn)
       break;
     GCP_path_entry_remove (entry->peer,
                            entry,
@@ -431,19 +435,20 @@ extend_path (struct CadetPeerPath *path,
     GNUNET_free (entry);
     path->entries[old_len + i] = NULL;
   }
-  if (NULL == hn)
+  if (NULL == path->hn)
   {
     /* none of the peers is interested in this path;
-       shrink path back */
+       shrink path back and re-attach. */
     GNUNET_array_grow (path->entries,
                        path->entries_length,
                        old_len);
+    path->hn = GCP_attach_path (path->entries[old_len - 1]->peer,
+                                path,
+                                old_len - 1,
+                                GNUNET_YES);
+    GNUNET_assert (NULL != path->hn);
     return;
   }
-  GCP_detach_path (path->entries[old_len-1]->peer,
-                   path,
-                   path->hn);
-  path->hn = hn;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Extended path %s\n",
        GCPP_2s (path));
index 070a0ac155064a33368d2328cefdaa96ccf460c8..136ab829780f0e7bca801236efe7c1d6d1af6d3c 100644 (file)
@@ -913,6 +913,7 @@ GCP_attach_path (struct CadetPeer *cp,
   GNUNET_CONTAINER_HeapCostType root_desirability;
   struct GNUNET_CONTAINER_HeapNode *hn;
 
+  GNUNET_assert (off == GCPP_get_length (path) - 1);
   GNUNET_assert (cp == GCPP_get_peer_at_offset (path,
                                                 off));
   if (NULL == cp->path_heap)
@@ -972,7 +973,7 @@ GCP_attach_path (struct CadetPeer *cp,
                                GCPP_get_length (root) - 1)) )
     {
       /* Got plenty of paths to this destination, and this is a low-quality
-         one that we don't care, allow it to die. */
+         one that we don't care about. Allow it to die. */
       GNUNET_assert (root ==
                      GNUNET_CONTAINER_heap_remove_root (cp->path_heap));
       GCPP_release (root);