Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sonar fixes
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Mon, 17 Apr 2023 08:01:01 +0000 (10:01 +0200)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Mon, 17 Apr 2023 08:23:19 +0000 (10:23 +0200)
- Undefined return value (that would happen on a platform without any host)
- Local variable shadowing
- Automatic memory management
- const, explicit, etc.

examples/cpp/dag-scheduling/s4u-dag-scheduling.cpp
src/mc/explo/DFSExplorer.cpp
src/mc/mc_exit.hpp
src/mc/sosp/Snapshot.cpp
src/mc/sosp/Snapshot_test.cpp

index ff68f38..c61f83f 100644 (file)
@@ -76,7 +76,7 @@ static double finish_on_at(const sg4::ExecPtr task, const sg4::Host* host)
 
 static sg4::Host* get_best_host(const sg4::ExecPtr exec)
 {
-  sg4::Host* best_host;
+  sg4::Host* best_host = nullptr;
   double min_EFT = std::numeric_limits<double>::max();
 
   for (const auto& host : sg4::Engine::get_instance()->get_all_hosts()) {
@@ -193,8 +193,8 @@ int main(int argc, char** argv)
   }
 
   /* Cleanup memory */
-  for (auto const& host : e.get_all_hosts())
-    delete host->get_data<double>();
+  for (auto const& h : e.get_all_hosts())
+    delete h->get_data<double>();
 
   XBT_INFO("Simulation Time: %f", simgrid_get_clock());
 
index bd8dbdc..cfacee4 100644 (file)
@@ -81,7 +81,7 @@ std::vector<std::string> DFSExplorer::get_textual_trace() // override
   for (auto const& transition : stack_.back()->get_recipe()) {
     trace.push_back(xbt::string_printf("%ld: %s", transition->aid_, transition->to_string().c_str()));
   }
-  if (auto* trans = stack_.back()->get_transition(); trans != nullptr)
+  if (const auto* trans = stack_.back()->get_transition(); trans != nullptr)
     trace.push_back(xbt::string_printf("%ld: %s", trans->aid_, trans->to_string().c_str()));
   return trace;
 }
index 3bb6cdd..234ea14 100644 (file)
@@ -23,7 +23,7 @@ enum class ExitStatus {
 
 struct McError : public std::exception {
   const ExitStatus value;
-  McError(ExitStatus v = ExitStatus::ERROR) : value(v) {}
+  explicit McError(ExitStatus v = ExitStatus::ERROR) : value(v) {}
 };
 } // namespace simgrid::mc
 
index eae958d..ee28945 100644 (file)
@@ -227,9 +227,9 @@ void Snapshot::add_region(RegionType type, const RemoteProcessMemory& memory, Ob
   else if (type == RegionType::Heap)
     xbt_assert(not object_info, "Unexpected object info for heap region.");
 
-  auto* region = new Region(page_store_, memory, type, start_addr, size);
+  auto region = std::make_unique<Region>(page_store_, memory, type, start_addr, size);
   region->object_info(object_info);
-  snapshot_regions_.push_back(std::unique_ptr<Region>(region));
+  snapshot_regions_.push_back(std::move(region));
 }
 
 void* Snapshot::read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, ReadOptions options) const
index d2f7d0e..09d865b 100644 (file)
@@ -25,8 +25,8 @@ public:
     size_t size;
     void* src;
     void* dstn;
-    Region* region0;
-    Region* region;
+    std::unique_ptr<Region> region0;
+    std::unique_ptr<Region> region;
   };
   static prologue_return prologue(int n); // common to the below 5 fxs
   static void read_whole_region();
@@ -68,18 +68,23 @@ snap_test_helper::prologue_return snap_test_helper::prologue(int n)
 
   // Init memory and take snapshots:
   init_memory(source, byte_size);
-  auto* region0 =
-      new simgrid::mc::Region(page_store_, *memory_.get(), simgrid::mc::RegionType::Data, source, byte_size);
+  auto region0 = std::make_unique<simgrid::mc::Region>(page_store_, *memory_.get(), simgrid::mc::RegionType::Data,
+                                                       source, byte_size);
   for (int i = 0; i < n; i += 2) {
     init_memory((char*)source + i * xbt_pagesize, xbt_pagesize);
   }
-  auto* region = new simgrid::mc::Region(page_store_, *memory_.get(), simgrid::mc::RegionType::Data, source, byte_size);
+  auto region = std::make_unique<simgrid::mc::Region>(page_store_, *memory_.get(), simgrid::mc::RegionType::Data,
+                                                      source, byte_size);
 
   void* destination = mmap(nullptr, byte_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
   INFO("Could not allocate destination memory");
   REQUIRE(destination != MAP_FAILED);
 
-  return {.size = byte_size, .src = source, .dstn = destination, .region0 = region0, .region = region};
+  return {.size    = byte_size,
+          .src     = source,
+          .dstn    = destination,
+          .region0 = std::move(region0),
+          .region  = std::move(region)};
 }
 
 void snap_test_helper::read_whole_region()
@@ -92,8 +97,6 @@ void snap_test_helper::read_whole_region()
 
     munmap(ret.dstn, ret.size);
     munmap(ret.src, ret.size);
-    delete ret.region0;
-    delete ret.region;
   }
 }
 
@@ -111,8 +114,6 @@ void snap_test_helper::read_region_parts()
     }
     munmap(ret.dstn, ret.size);
     munmap(ret.src, ret.size);
-    delete ret.region0;
-    delete ret.region;
   }
 }
 
@@ -122,12 +123,10 @@ void snap_test_helper::compare_whole_region()
     prologue_return ret = prologue(n);
 
     INFO("Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
-    REQUIRE(MC_snapshot_region_memcmp(ret.src, ret.region0, ret.src, ret.region, ret.size));
+    REQUIRE(MC_snapshot_region_memcmp(ret.src, ret.region0.get(), ret.src, ret.region.get(), ret.size));
 
     munmap(ret.dstn, ret.size);
     munmap(ret.src, ret.size);
-    delete ret.region0;
-    delete ret.region;
   }
 }
 
@@ -141,13 +140,11 @@ void snap_test_helper::compare_region_parts()
       size_t size   = simgrid::xbt::random::uniform_int(0, ret.size - offset - 1);
 
       INFO("Mismatch in MC_snapshot_region_memcmp()");
-      REQUIRE(not MC_snapshot_region_memcmp((char*)ret.src + offset, ret.region, (char*)ret.src + offset, ret.region,
-                                            size));
+      REQUIRE(not MC_snapshot_region_memcmp((char*)ret.src + offset, ret.region.get(), (char*)ret.src + offset,
+                                            ret.region.get(), size));
     }
     munmap(ret.dstn, ret.size);
     munmap(ret.src, ret.size);
-    delete ret.region0;
-    delete ret.region;
   }
 }
 
@@ -163,8 +160,6 @@ void snap_test_helper::read_pointer()
 
   munmap(ret.dstn, ret.size);
   munmap(ret.src, ret.size);
-  delete ret.region0;
-  delete ret.region;
 }
 
 /*************** End: class snap_test_helper *****************************/