Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Better fix for the security warning from sonar: hide the char* buffer
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 12 Feb 2022 09:36:00 +0000 (10:36 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Sat, 12 Feb 2022 09:36:00 +0000 (10:36 +0100)
src/kernel/actor/SimcallObserver.cpp
src/kernel/actor/SimcallObserver.hpp
src/mc/remote/AppSide.cpp
src/mc/remote/mc_protocol.h

index 03ce516..aa457e4 100644 (file)
@@ -28,15 +28,10 @@ bool RandomSimcall::depends(SimcallObserver* other)
 {
   return get_issuer() == other->get_issuer();
 }
-void RandomSimcall::serialize(Simcall& type, char* buffer)
+void RandomSimcall::serialize(Simcall& type, std::stringstream& stream)
 {
   type = Simcall::RANDOM;
-  std::stringstream stream;
-
   stream << min_ << ' ' << max_;
-  xbt_assert(stream.str().size() < SIMCALL_SERIALIZATION_BUFFER_SIZE,
-             "The serialized simcall is too large for the buffer. Please fix the code.");
-  strncpy(buffer, stream.str().c_str(), SIMCALL_SERIALIZATION_BUFFER_SIZE);
 }
 
 bool MutexSimcall::depends(SimcallObserver* other)
@@ -229,9 +224,8 @@ bool ActivityTestSimcall::depends(SimcallObserver* other)
 
   return true;
 }
-void ActivityWaitSimcall::serialize(Simcall& type, char* buffer)
+void ActivityWaitSimcall::serialize(Simcall& type, std::stringstream& stream)
 {
-  std::stringstream stream;
   if (auto* comm = dynamic_cast<activity::CommImpl*>(activity_)) {
     type = Simcall::COMM_WAIT;
     stream << (timeout_ > 0) << ' ' << comm;
@@ -239,12 +233,8 @@ void ActivityWaitSimcall::serialize(Simcall& type, char* buffer)
     stream << ' ' << (comm->dst_actor_ != nullptr ? comm->dst_actor_->get_pid() : -1);
     stream << ' ' << comm->get_mailbox_id();
     stream << ' ' << (void*)comm->src_buff_ << ' ' << (void*)comm->dst_buff_ << ' ' << comm->src_buff_size_;
-    xbt_assert(stream.str().size() < SIMCALL_SERIALIZATION_BUFFER_SIZE,
-               "The serialized simcall is too large for the buffer. Please fix the code.");
-    strncpy(buffer, stream.str().c_str(), SIMCALL_SERIALIZATION_BUFFER_SIZE);
   } else {
     type = Simcall::UNKNOWN;
-    buffer[0] = '\0';
   }
 }
 
@@ -355,42 +345,18 @@ void ActivityWaitanySimcall::prepare(int times_considered)
   next_value_ = times_considered;
 }
 
-void CommIsendSimcall::serialize(Simcall& type, char* buffer)
+void CommIsendSimcall::serialize(Simcall& type, std::stringstream& stream)
 {
   type = Simcall::ISEND;
-  std::stringstream stream;
   stream << mbox_->get_id() << ' ' << (void*)src_buff_ << ' ' << src_buff_size_;
-  xbt_assert(stream.str().size() < SIMCALL_SERIALIZATION_BUFFER_SIZE,
-             "The serialized simcall is too large for the buffer. Please fix the code.");
-  strncpy(buffer, stream.str().c_str(), SIMCALL_SERIALIZATION_BUFFER_SIZE);
   XBT_DEBUG("SendObserver mbox:%u buff:%p size:%zu", mbox_->get_id(), src_buff_, src_buff_size_);
 }
 
-void CommIrecvSimcall::serialize(Simcall& type, char* buffer)
+void CommIrecvSimcall::serialize(Simcall& type, std::stringstream& stream)
 {
   type = Simcall::IRECV;
-  std::stringstream stream;
   stream << mbox_->get_id() << dst_buff_;
-  xbt_assert(stream.str().size() < SIMCALL_SERIALIZATION_BUFFER_SIZE,
-             "The serialized simcall is too large for the buffer. Please fix the code.");
-  strncpy(buffer, stream.str().c_str(), SIMCALL_SERIALIZATION_BUFFER_SIZE);
-}
-
-
-/*
-std::string CommIrecvSimcall::to_string(int times_considered) const
-{
-  std::string res = SimcallObserver::to_string(times_considered) + "iRecv(";
-  res += xbt::string_printf("dst=(%ld)%s (%s)", get_issuer()->get_pid(), get_issuer()->get_host()->get_cname(),
-                            get_issuer()->get_cname());
-  res += ", buff=" + (XBT_LOG_ISENABLED(mc_observer, xbt_log_priority_verbose) ? xbt::string_printf("%p", dst_buff_)
-                                                                               : "(verbose only)");
-  res += ", size=" + (XBT_LOG_ISENABLED(mc_observer, xbt_log_priority_verbose) ? std::to_string(*dst_buff_size_)
-                                                                               : "(verbose only)");
-  res += ")";
-  return res;
 }
-*/
 
 } // namespace actor
 } // namespace kernel
index 2666b9c..f579ffa 100644 (file)
@@ -12,8 +12,6 @@
 
 #include <string>
 
-#define SIMCALL_SERIALIZATION_BUFFER_SIZE 2048
-
 namespace simgrid {
 namespace kernel {
 namespace actor {
@@ -61,8 +59,8 @@ public:
   /** Computes the dependency relation */
   virtual bool depends(SimcallObserver* other);
 
-  /** Serialize to the given buffer */
-  virtual void serialize(Simcall& type, char* buffer) { type = Simcall::UNKNOWN; }
+  /** Serialize to the given string buffer */
+  virtual void serialize(Simcall& type, std::stringstream& stream) { type = Simcall::UNKNOWN; }
 
   /** Some simcalls may only be observable under some conditions.
    * Most simcalls are not visible from the MC because they don't have an observer at all. */
@@ -96,7 +94,7 @@ public:
     res->next_value_ = next_value_;
     return res;
   }
-  void serialize(Simcall& type, char* buffer) override;
+  void serialize(Simcall& type, std::stringstream& stream) override;
   int get_max_consider() const override;
   void prepare(int times_considered) override;
   std::string dot_label(int times_considered) const override;
@@ -214,7 +212,7 @@ public:
   {
   }
   SimcallObserver* clone() override { return new ActivityWaitSimcall(get_issuer(), activity_, timeout_); }
-  void serialize(Simcall& type, char* buffer) override;
+  void serialize(Simcall& type, std::stringstream& stream) override;
   bool is_visible() const override { return true; }
   bool is_enabled() const override;
   std::string dot_label(int times_considered) const override;
@@ -276,7 +274,7 @@ public:
       , copy_data_fun_(copy_data_fun)
   {
   }
-  void serialize(Simcall& type, char* buffer) override;
+  void serialize(Simcall& type, std::stringstream& stream) override;
   CommIsendSimcall* clone() override
   {
     return new CommIsendSimcall(get_issuer(), mbox_, payload_size_, rate_, src_buff_, src_buff_size_, match_fun_,
@@ -325,7 +323,7 @@ public:
     return new CommIrecvSimcall(get_issuer(), mbox_, dst_buff_, dst_buff_size_, match_fun_, copy_data_fun_, payload_,
                                 rate_);
   }
-  void serialize(Simcall& type, char* buffer) override;
+  void serialize(Simcall& type, std::stringstream& stream) override;
   bool is_visible() const override { return true; }
   std::string dot_label(int times_considered) const override
   {
index 4517e70..e0026c0 100644 (file)
@@ -103,8 +103,16 @@ void AppSide::handle_simcall_execute(const s_mc_message_simcall_execute_t* messa
   s_mc_message_simcall_execute_answer_t answer;
   memset(&answer, 0, sizeof(answer));
   answer.type = MessageType::SIMCALL_EXECUTE_ANSWER;
-  if (actor->simcall_.observer_ != nullptr)
-    actor->simcall_.observer_->serialize(answer.simcall, answer.buffer);
+  if (actor->simcall_.observer_ != nullptr) {
+    std::stringstream stream;
+    actor->simcall_.observer_->serialize(answer.simcall, stream);
+    xbt_assert(stream.str().size() < sizeof(answer.buffer),
+               "The serialized simcall is too large for the buffer. Please fix the code.");
+    strncpy(answer.buffer, stream.str().c_str(), SIMCALL_SERIALIZATION_BUFFER_SIZE);
+  } else {
+    answer.simcall = kernel::actor::SimcallObserver::Simcall::UNKNOWN;
+  }
+
   XBT_DEBUG("send SIMCALL_EXECUTE_ANSWER(%s) ~> %s '%s'", actor->get_cname(),
             simgrid::kernel::actor::SimcallObserver::to_c_str(answer.simcall), answer.buffer);
   xbt_assert(channel_.send(answer) == 0, "Could not send response");
index 1a1fed3..8def3da 100644 (file)
@@ -35,6 +35,8 @@ XBT_DECLARE_ENUM_CLASS(MessageType, NONE, INITIAL_ADDRESSES, CONTINUE, IGNORE_HE
                        SIMCALL_EXECUTE_ANSWER, SIMCALL_IS_VISIBLE, SIMCALL_IS_VISIBLE_ANSWER, SIMCALL_DOT_LABEL,
                        SIMCALL_DOT_LABEL_ANSWER, ASSERTION_FAILED, ACTOR_ENABLED, ACTOR_ENABLED_REPLY, FINALIZE);
 
+#define SIMCALL_SERIALIZATION_BUFFER_SIZE 2048
+
 } // namespace mc
 } // namespace simgrid