From: Martin Quinson Date: Mon, 17 Apr 2023 08:01:01 +0000 (+0200) Subject: sonar fixes X-Git-Tag: v3.34~154 X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/a35cd80abaa1a7679b091c8e1aa5180ad3d85bda sonar fixes - Undefined return value (that would happen on a platform without any host) - Local variable shadowing - Automatic memory management - const, explicit, etc. --- diff --git a/examples/cpp/dag-scheduling/s4u-dag-scheduling.cpp b/examples/cpp/dag-scheduling/s4u-dag-scheduling.cpp index ff68f381af..c61f83f877 100644 --- a/examples/cpp/dag-scheduling/s4u-dag-scheduling.cpp +++ b/examples/cpp/dag-scheduling/s4u-dag-scheduling.cpp @@ -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::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(); + for (auto const& h : e.get_all_hosts()) + delete h->get_data(); XBT_INFO("Simulation Time: %f", simgrid_get_clock()); diff --git a/src/mc/explo/DFSExplorer.cpp b/src/mc/explo/DFSExplorer.cpp index bd8dbdc9a0..cfacee45a3 100644 --- a/src/mc/explo/DFSExplorer.cpp +++ b/src/mc/explo/DFSExplorer.cpp @@ -81,7 +81,7 @@ std::vector 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; } diff --git a/src/mc/mc_exit.hpp b/src/mc/mc_exit.hpp index 3bb6cdde0b..234ea1466a 100644 --- a/src/mc/mc_exit.hpp +++ b/src/mc/mc_exit.hpp @@ -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 diff --git a/src/mc/sosp/Snapshot.cpp b/src/mc/sosp/Snapshot.cpp index eae958d947..ee28945420 100644 --- a/src/mc/sosp/Snapshot.cpp +++ b/src/mc/sosp/Snapshot.cpp @@ -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(page_store_, memory, type, start_addr, size); region->object_info(object_info); - snapshot_regions_.push_back(std::unique_ptr(region)); + snapshot_regions_.push_back(std::move(region)); } void* Snapshot::read_bytes(void* buffer, std::size_t size, RemotePtr address, ReadOptions options) const diff --git a/src/mc/sosp/Snapshot_test.cpp b/src/mc/sosp/Snapshot_test.cpp index d2f7d0e24c..09d865bfac 100644 --- a/src/mc/sosp/Snapshot_test.cpp +++ b/src/mc/sosp/Snapshot_test.cpp @@ -25,8 +25,8 @@ public: size_t size; void* src; void* dstn; - Region* region0; - Region* region; + std::unique_ptr region0; + std::unique_ptr 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(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(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 *****************************/