From 263b12c1c44e482ed8ff97e29fb270b3abb35bb2 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 14 May 2021 22:10:16 +0200 Subject: [PATCH] stop using reinterpret_cast --- src/cs_bcode.cc | 6 ++++-- src/cs_gen.cc | 14 ++++++++++---- src/cs_strman.cc | 26 +++++++++++++++++++------- src/cs_vm.cc | 34 +++++++++++++++++++--------------- 4 files changed, 52 insertions(+), 28 deletions(-) diff --git a/src/cs_bcode.cc b/src/cs_bcode.cc index 1e0efbe..d59b114 100644 --- a/src/cs_bcode.cc +++ b/src/cs_bcode.cc @@ -90,7 +90,8 @@ std::uint32_t *bcode_alloc(internal_state *cs, std::size_t sz) { auto a = std_allocator{cs}; std::size_t hdrs = sizeof(bcode_hdr) / sizeof(std::uint32_t); auto p = a.allocate(sz + hdrs - 1); - bcode_hdr *hdr = reinterpret_cast(p); + bcode_hdr *hdr; + std::memcpy(&hdr, &p, sizeof(hdr)); hdr->cs = cs; hdr->asize = sz + hdrs - 1; return p + hdrs - 1; @@ -99,7 +100,8 @@ std::uint32_t *bcode_alloc(internal_state *cs, std::size_t sz) { /* bc's address must be the 'init' member of the header */ static inline void bcode_free(std::uint32_t *bc) { auto *rp = bc + 1 - (sizeof(bcode_hdr) / sizeof(std::uint32_t)); - bcode_hdr *hdr = reinterpret_cast(rp); + bcode_hdr *hdr; + std::memcpy(&hdr, &rp, sizeof(hdr)); std_allocator{hdr->cs}.deallocate(rp, hdr->asize); } diff --git a/src/cs_gen.cc b/src/cs_gen.cc index cbf4e0d..9620913 100644 --- a/src/cs_gen.cc +++ b/src/cs_gen.cc @@ -27,7 +27,10 @@ std::uint32_t gen_state::peek(std::size_t idx) const { bcode_ref gen_state::steal_ref() { auto *cp = bcode_alloc(ts.istate, code.size()); std::memcpy(cp, code.data(), code.size() * sizeof(std::uint32_t)); - return bcode_p::make_ref(reinterpret_cast(cp + 1)); + bcode *b; + cp += 1; + std::memcpy(&b, &cp, sizeof(b)); + return bcode_p::make_ref(b); } void gen_state::gen_pop() { @@ -179,8 +182,10 @@ void gen_state::gen_val_string(std::string_view v) { return; } code.push_back(BC_INST_VAL | BC_RET_STRING | std::uint32_t(vsz << 8)); - auto it = reinterpret_cast(v.data()); - code.append(it, it + (v.size() / sizeof(std::uint32_t))); + std::uint32_t *wp; + auto *sp = v.data(); + std::memcpy(&wp, &sp, sizeof(wp)); + code.append(wp, wp + (v.size() / sizeof(std::uint32_t))); std::size_t esz = v.size() % sizeof(std::uint32_t); char c[sizeof(std::uint32_t)] = {0}; std::memcpy(c, v.data() + v.size() - esz, esz); @@ -208,7 +213,8 @@ static void gen_str_filter( memset(&buf[len], 0, sizeof(std::uint32_t) - len % sizeof(std::uint32_t)); /* set the actual length */ code.back() |= (len << 8); - auto *ubuf = reinterpret_cast(buf); + std::uint32_t *ubuf; + std::memcpy(&ubuf, &buf, sizeof(ubuf)); code.append(ubuf, ubuf + ((len / sizeof(std::uint32_t)) + 1)); al.deallocate(buf, nwords * sizeof(std::uint32_t)); } diff --git a/src/cs_strman.cc b/src/cs_strman.cc index 4dc0621..00884b9 100644 --- a/src/cs_strman.cc +++ b/src/cs_strman.cc @@ -1,4 +1,5 @@ #include +#include #include #include "cs_strman.hh" @@ -13,9 +14,9 @@ struct string_ref_state { }; inline string_ref_state *get_ref_state(char const *ptr) { - return const_cast( - reinterpret_cast(ptr) - ) - 1; + string_ref_state *r; + std::memcpy(&r, &ptr, sizeof(r)); + return r - 1; } char const *string_pool::add(std::string_view str) { @@ -26,7 +27,10 @@ char const *string_pool::add(std::string_view str) { /* having a null pointer is the same as non-existence */ if (st) { ++st->refcount; - return reinterpret_cast(st + 1); + st += 1; + char const *r; + std::memcpy(&r, &st, sizeof(r)); + return r; } } /* not present: allocate brand new data */ @@ -55,7 +59,10 @@ string_ref string_pool::steal(char *ptr) { if (st) { /* the buffer is superfluous now */ cstate->alloc(ss, ss->length + sizeof(string_ref_state) + 1, 0); - return string_ref{reinterpret_cast(st + 1)}; + st += 1; + char const *rp; + std::memcpy(&rp, &st, sizeof(rp)); + return string_ref{rp}; } } ss->refcount = 0; /* string_ref will increment it */ @@ -91,7 +98,10 @@ char const *string_pool::find(std::string_view str) const { if (it == counts.end()) { return nullptr; } - return reinterpret_cast(it->second + 1); + auto *sp = it->second + 1; + char const *rp; + std::memcpy(&rp, &sp, sizeof(rp)); + return rp; } std::string_view string_pool::get(char const *ptr) const { @@ -107,7 +117,9 @@ char *string_pool::alloc_buf(std::size_t len) const { sst->length = len; sst->refcount = 1; /* pre-terminate */ - auto *strp = reinterpret_cast(sst + 1); + char *strp; + sst += 1; + std::memcpy(&strp, &sst, sizeof(strp)); strp[len] = '\0'; /* now the user can fill it */ return strp; diff --git a/src/cs_vm.cc b/src/cs_vm.cc index 67c5712..91c7e1c 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -359,24 +359,27 @@ std::uint32_t *vm_exec( switch (op & BC_INST_RET_MASK) { case BC_RET_STRING: { auto len = op >> 8; - args.emplace_back().set_string(std::string_view{ - reinterpret_cast(code), len - }, cs); + char const *str; + std::memcpy(&str, &code, sizeof(str)); + std::string_view sv{str, len}; + args.emplace_back().set_string(sv, cs); code += len / sizeof(std::uint32_t) + 1; continue; } - case BC_RET_INT: - args.emplace_back().set_integer( - *reinterpret_cast(code) - ); + case BC_RET_INT: { + integer_type i; + std::memcpy(&i, code, sizeof(i)); + args.emplace_back().set_integer(i); code += bc_store_size; continue; - case BC_RET_FLOAT: - args.emplace_back().set_float( - *reinterpret_cast(code) - ); + } + case BC_RET_FLOAT: { + float_type f; + std::memcpy(&f, code, sizeof(f)); + args.emplace_back().set_float(f); code += bc_store_size; continue; + } default: break; } @@ -501,10 +504,11 @@ std::uint32_t *vm_exec( case BC_INST_BLOCK: { std::uint32_t len = op >> 8; - args.emplace_back().set_code(bcode_p::make_ref( - reinterpret_cast(code + 1) - )); - code += len; + bcode *b; + code += 1; + std::memcpy(&b, &code, sizeof(b)); + args.emplace_back().set_code(bcode_p::make_ref(b)); + code += len - 1; continue; }