Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix use-after-free.
[simgrid.git] / examples / s4u / dht-chord / node.cpp
index 79aef92..d97b6b1 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2016. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -47,7 +47,7 @@ Node::Node(std::vector<std::string> args)
 
   // initialize my node
   id_                = std::stoi(args[1]);
-  stream             = simgrid::s4u::this_actor::host()->extension<HostChord>()->getStream();
+  stream             = simgrid::s4u::this_actor::getHost()->extension<HostChord>()->getStream();
   mailbox_           = simgrid::s4u::Mailbox::byName(std::to_string(id_));
   next_finger_to_fix = 0;
   fingers_           = new int[nb_bits];
@@ -120,8 +120,8 @@ void Node::notifyAndQuit()
     }
   }
 
-  if (pred_id_ != -1) {
-    // send the SUCCESSOR_LEAVING to our predecessor (only if I have one)
+  if (pred_id_ != -1 && pred_id_ != id_) {
+    // send the SUCCESSOR_LEAVING to our predecessor (only if I have one that is not me)
     ChordMessage* succ_msg = new ChordMessage(SUCCESSOR_LEAVING);
     succ_msg->request_id   = fingers_[0];
     succ_msg->answer_to    = mailbox_;
@@ -230,19 +230,19 @@ void Node::checkPredecessor()
   }
   // receive the answer
   XBT_DEBUG("Sent 'Predecessor Alive' request to %d, waiting for the answer on my mailbox '%s'", pred_id_,
-            message->answer_to->name());
+            message->answer_to->getName());
   simgrid::s4u::CommPtr comm = return_mailbox->get_async(&data);
 
   try {
     comm->wait(timeout);
     XBT_DEBUG("Received the answer to my 'Predecessor Alive': my predecessor %d is alive", pred_id_);
+    delete static_cast<ChordMessage*>(data);
   } catch (xbt_ex& e) {
     if (e.category == timeout_error) {
       XBT_DEBUG("Failed to receive the answer to my 'Predecessor Alive' request");
       pred_id_ = -1;
     }
   }
-  delete message;
 }
 
 /* Asks its predecessor to a remote node
@@ -275,7 +275,7 @@ int Node::remoteGetPredecessor(int ask_to)
 
   // receive the answer
   XBT_DEBUG("Sent 'Get Predecessor' request to %d, waiting for the answer on my mailbox '%s'", ask_to,
-            message->answer_to->name());
+            message->answer_to->getName());
   simgrid::s4u::CommPtr comm = return_mailbox->get_async(&data);
 
   try {
@@ -398,13 +398,10 @@ void Node::stabilize()
   XBT_DEBUG("Stabilizing node");
 
   // get the predecessor of my immediate successor
-  int candidate_id;
+  int candidate_id = pred_id_;
   int successor_id = fingers_[0];
-  if (successor_id != id_) {
+  if (successor_id != id_)
     candidate_id = remoteGetPredecessor(successor_id);
-  } else {
-    candidate_id = pred_id_;
-  }
 
   // this node is a candidate to become my new successor
   if (candidate_id != -1 && is_in_interval(candidate_id, id_ + 1, successor_id - 1)) {
@@ -430,7 +427,8 @@ void Node::handleMessage(ChordMessage* message)
       message->type = FIND_SUCCESSOR_ANSWER;
       message->answer_id = fingers_[0];
       XBT_DEBUG("Sending back a 'Find Successor Answer' to %s (mailbox %s): the successor of %d is %d",
-          message->issuer_host_name.c_str(), message->answer_to->name(), message->request_id, message->answer_id);
+                message->issuer_host_name.c_str(), message->answer_to->getName(), message->request_id,
+                message->answer_id);
       message->answer_to->put_init(message, 10)->detach();
     } else {
       // otherwise, forward the request to the closest preceding finger in my table
@@ -447,7 +445,7 @@ void Node::handleMessage(ChordMessage* message)
     message->type = GET_PREDECESSOR_ANSWER;
     message->answer_id = pred_id_;
     XBT_DEBUG("Sending back a 'Get Predecessor Answer' to %s via mailbox '%s': my predecessor is %d",
-        message->issuer_host_name.c_str(), message->answer_to->name(), message->answer_id);
+              message->issuer_host_name.c_str(), message->answer_to->getName(), message->answer_id);
     message->answer_to->put_init(message, 10)->detach();
     break;
 
@@ -484,8 +482,8 @@ void Node::handleMessage(ChordMessage* message)
   case PREDECESSOR_ALIVE:
     XBT_DEBUG("Receiving a 'Predecessor Alive' request from %s", message->issuer_host_name.c_str());
     message->type = PREDECESSOR_ALIVE_ANSWER;
-    XBT_DEBUG("Sending back a 'Predecessor Alive Answer' to %s (mailbox %s)",
-        message->issuer_host_name.c_str(), message->answer_to->name());
+    XBT_DEBUG("Sending back a 'Predecessor Alive Answer' to %s (mailbox %s)", message->issuer_host_name.c_str(),
+              message->answer_to->getName());
     message->answer_to->put_init(message, 10)->detach();
     break;