From b0d07f67c6153ee2689bc22486c95becf4286493 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Fri, 20 Dec 2019 22:58:48 +0100 Subject: [PATCH] [sonar] Fix casts to pointer type without const qualifier. --- examples/deprecated/msg/dht-kademlia/answer.c | 4 ++-- examples/deprecated/simdag/daxload/sd_daxload.c | 2 +- src/mc/compare.cpp | 13 +++++++------ src/mc/mc_smx.cpp | 3 ++- src/mc/sosp/ChunkedData.hpp | 2 +- src/mc/sosp/PageStore.cpp | 2 +- src/mc/sosp/PageStore.hpp | 4 ++-- src/mc/sosp/Region.cpp | 8 ++++---- teshsuite/simdag/availability/availability.c | 2 +- teshsuite/simdag/basic-link-test/basic-link-test.c | 6 +++--- 10 files changed, 24 insertions(+), 22 deletions(-) diff --git a/examples/deprecated/msg/dht-kademlia/answer.c b/examples/deprecated/msg/dht-kademlia/answer.c index d420bb9975..fb3bcc2675 100644 --- a/examples/deprecated/msg/dht-kademlia/answer.c +++ b/examples/deprecated/msg/dht-kademlia/answer.c @@ -69,8 +69,8 @@ unsigned int answer_merge(answer_t destination, answer_t source) /** Helper to sort answer_t objects */ static int _answer_sort_function(const void *e1, const void *e2) { - node_contact_t c1 = *(void **) e1; - node_contact_t c2 = *(void **) e2; + node_contact_t c1 = *(const node_contact_t*)e1; + node_contact_t c2 = *(const node_contact_t*)e2; if (c1->distance == c2->distance) return 0; else diff --git a/examples/deprecated/simdag/daxload/sd_daxload.c b/examples/deprecated/simdag/daxload/sd_daxload.c index 388b69094e..098c3e894e 100644 --- a/examples/deprecated/simdag/daxload/sd_daxload.c +++ b/examples/deprecated/simdag/daxload/sd_daxload.c @@ -15,7 +15,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to this SimDag example"); static int name_compare_hosts(const void *n1, const void *n2) { - return strcmp(sg_host_get_name(*(sg_host_t *) n1), sg_host_get_name(*(sg_host_t *) n2)); + return strcmp(sg_host_get_name(*(const sg_host_t*)n1), sg_host_get_name(*(const sg_host_t*)n2)); } int main(int argc, char **argv) diff --git a/src/mc/compare.cpp b/src/mc/compare.cpp index 159d83c374..51c00fec9e 100644 --- a/src/mc/compare.cpp +++ b/src/mc/compare.cpp @@ -767,8 +767,8 @@ static bool heap_area_differ(StateComparator& state, const void* area1, const vo } // Get block number: - block1 = ((char*)area1 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1; - block2 = ((char*)area2 - (char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1; + block1 = ((const char*)area1 - (const char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1; + block2 = ((const char*)area2 - (const char*)state.std_heap_copy.heapbase) / BLOCKSIZE + 1; // If either block is a stack block: if (is_block_stack((int) block1) && is_block_stack((int) block2)) { @@ -779,8 +779,9 @@ static bool heap_area_differ(StateComparator& state, const void* area1, const vo } // If either block is not in the expected area of memory: - if (((char*)area1 < (char*)state.std_heap_copy.heapbase) || (block1 > (ssize_t)state.processStates[0].heapsize) || - (block1 < 1) || ((char*)area2 < (char*)state.std_heap_copy.heapbase) || + if (((const char*)area1 < (const char*)state.std_heap_copy.heapbase) || + (block1 > (ssize_t)state.processStates[0].heapsize) || (block1 < 1) || + ((const char*)area2 < (const char*)state.std_heap_copy.heapbase) || (block2 > (ssize_t)state.processStates[1].heapsize) || (block2 < 1)) { return true; } @@ -927,8 +928,8 @@ static bool heap_area_differ(StateComparator& state, const void* area1, const vo } // Type inference from the block type. else if (state.types_<1>(block1, frag1) != nullptr || state.types_<2>(block2, frag2) != nullptr) { - offset1 = (char*)area1 - (char*)real_addr_frag1; - offset2 = (char*)area2 - (char*)real_addr_frag2; + offset1 = (const char*)area1 - (const char*)real_addr_frag1; + offset2 = (const char*)area2 - (const char*)real_addr_frag2; if (state.types_<1>(block1, frag1) != nullptr && state.types_<2>(block2, frag2) != nullptr) { new_type1 = get_offset_type(real_addr_frag1, state.types_<1>(block1, frag1), offset1, size, snapshot1); diff --git a/src/mc/mc_smx.cpp b/src/mc/mc_smx.cpp index 3570f8eb4e..e57e6bd7a8 100644 --- a/src/mc/mc_smx.cpp +++ b/src/mc/mc_smx.cpp @@ -123,7 +123,8 @@ const char* MC_smx_actor_get_host_name(smx_actor_t actor) // Read the simgrid::xbt::string in the MCed process: simgrid::mc::ActorInformation* info = actor_info_cast(actor); - auto remote_string_address = remote((simgrid::xbt::string_data*)&actor->get_host()->get_name()); + auto remote_string_address = + remote(reinterpret_cast(&actor->get_host()->get_name())); simgrid::xbt::string_data remote_string = process->read(remote_string_address); char hostname[remote_string.len]; process->read_bytes(hostname, remote_string.len + 1, remote(remote_string.data)); diff --git a/src/mc/sosp/ChunkedData.hpp b/src/mc/sosp/ChunkedData.hpp index ac4e24a7d8..2545954bc3 100644 --- a/src/mc/sosp/ChunkedData.hpp +++ b/src/mc/sosp/ChunkedData.hpp @@ -82,7 +82,7 @@ public: const std::size_t* pagenos() const { return pagenos_.data(); } /** Get a a pointer to a chunk */ - const void* page(std::size_t i) const { return store_->get_page(pagenos_[i]); } + void* page(std::size_t i) const { return store_->get_page(pagenos_[i]); } ChunkedData(PageStore& store, AddressSpace& as, RemotePtr addr, std::size_t page_count); }; diff --git a/src/mc/sosp/PageStore.cpp b/src/mc/sosp/PageStore.cpp index 5126851e57..e3702c83f8 100644 --- a/src/mc/sosp/PageStore.cpp +++ b/src/mc/sosp/PageStore.cpp @@ -166,7 +166,7 @@ std::size_t PageStore::store_page(void* page) // Otherwise, a new page is allocated in the page store and the content of the page is `memcpy()`-ed to this new page. std::size_t pageno = alloc_page(); xbt_assert(this->page_counts_[pageno] == 0, "Allocated page is already used"); - void* snapshot_page = (void*)this->get_page(pageno); + void* snapshot_page = this->get_page(pageno); memcpy(snapshot_page, page, xbt_pagesize); page_set.insert(pageno); page_counts_[pageno]++; diff --git a/src/mc/sosp/PageStore.hpp b/src/mc/sosp/PageStore.hpp index c78f57509f..0853b6d905 100644 --- a/src/mc/sosp/PageStore.hpp +++ b/src/mc/sosp/PageStore.hpp @@ -141,7 +141,7 @@ public: * @param pageno Number of the memory page in the store * @return Start of the page */ - const void* get_page(std::size_t pageno) const; + void* get_page(std::size_t pageno) const; // Debug/test methods @@ -169,7 +169,7 @@ XBT_ALWAYS_INLINE void PageStore::ref_page(size_t pageno) ++this->page_counts_[pageno]; } -XBT_ALWAYS_INLINE const void* PageStore::get_page(std::size_t pageno) const +XBT_ALWAYS_INLINE void* PageStore::get_page(std::size_t pageno) const { return (void*)simgrid::mc::mmu::join(pageno, (std::uintptr_t)this->memory_); } diff --git a/src/mc/sosp/Region.cpp b/src/mc/sosp/Region.cpp index 3208437e33..9d04b83341 100644 --- a/src/mc/sosp/Region.cpp +++ b/src/mc/sosp/Region.cpp @@ -49,7 +49,7 @@ static XBT_ALWAYS_INLINE void* mc_translate_address_region(uintptr_t addr, simgr auto split = simgrid::mc::mmu::split(addr - region->start().address()); auto pageno = split.first; auto offset = split.second; - const void* snapshot_page = region->get_chunks().page(pageno); + void* snapshot_page = region->get_chunks().page(pageno); return (char*)snapshot_page + offset; } @@ -58,7 +58,7 @@ void* Region::read(void* target, const void* addr, std::size_t size) xbt_assert(contain(simgrid::mc::remote(addr)), "Trying to read out of the region boundary."); // Last byte of the region: - void* end = (char*)addr + size - 1; + const void* end = (const char*)addr + size - 1; if (simgrid::mc::mmu::same_chunk((std::uintptr_t)addr, (std::uintptr_t)end)) { // The memory is contained in a single page: return mc_translate_address_region((uintptr_t)addr, this); @@ -78,9 +78,9 @@ void* Region::read(void* target, const void* addr, std::size_t size) while (simgrid::mc::mmu::split((std::uintptr_t)addr).first != page_end) { void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, this); void* next_page = (void*)simgrid::mc::mmu::join(simgrid::mc::mmu::split((std::uintptr_t)addr).first + 1, 0); - size_t readable = (char*)next_page - (char*)addr; + size_t readable = (char*)next_page - (const char*)addr; memcpy(dest, snapshot_addr, readable); - addr = (char*)addr + readable; + addr = (const char*)addr + readable; dest = (char*)dest + readable; size -= readable; } diff --git a/teshsuite/simdag/availability/availability.c b/teshsuite/simdag/availability/availability.c index 443a4f1b05..405124237c 100644 --- a/teshsuite/simdag/availability/availability.c +++ b/teshsuite/simdag/availability/availability.c @@ -12,7 +12,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging for the current example"); static int name_compare_hosts(const void *n1, const void *n2) { - return strcmp(sg_host_get_name(*(sg_host_t *) n1), sg_host_get_name(*(sg_host_t *) n2)); + return strcmp(sg_host_get_name(*(const sg_host_t*)n1), sg_host_get_name(*(const sg_host_t*)n2)); } static void scheduleDAX(xbt_dynar_t dax) diff --git a/teshsuite/simdag/basic-link-test/basic-link-test.c b/teshsuite/simdag/basic-link-test/basic-link-test.c index 4fce8a8997..a95b86130d 100644 --- a/teshsuite/simdag/basic-link-test/basic-link-test.c +++ b/teshsuite/simdag/basic-link-test/basic-link-test.c @@ -10,12 +10,12 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(basic_link_test, sd, "SimDag test basic_link_test"); static int cmp_link(const void*a, const void*b) { - return strcmp(sg_link_name(*(SD_link_t*)a) , sg_link_name(*(SD_link_t*)b)); + return strcmp(sg_link_name(*(const SD_link_t*)a), sg_link_name(*(const SD_link_t*)b)); } int main(int argc, char **argv) { - const char *user_data = "some user_data"; + char user_data[] = "some user_data"; /* initialization of SD */ SD_init(&argc, argv); @@ -30,7 +30,7 @@ int main(int argc, char **argv) for (int i=0; i < count; i++){ XBT_INFO("%s: latency = %.5f, bandwidth = %f", sg_link_name(links[i]), sg_link_latency(links[i]), sg_link_bandwidth(links[i])); - sg_link_data_set(links[i], (void*) user_data); + sg_link_data_set(links[i], user_data); xbt_assert(!strcmp(user_data, (const char*)sg_link_data(links[i])),"User data was corrupted."); } xbt_free(links); -- 2.20.1