From 3d22b8de8bead66531843f1a5649e196068ac192 Mon Sep 17 00:00:00 2001 From: surtur Date: Sat, 8 Jan 2022 07:06:31 +0100 Subject: [PATCH] generator: fix memmove UB warnings --- generator.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/generator.cpp b/generator.cpp index 0eefeb2..de51f1b 100644 --- a/generator.cpp +++ b/generator.cpp @@ -51,7 +51,7 @@ auto Generator::reseed(const std::string& s) -> void { try { std::lock_guard lg(mtx); std::string a{fortuna::Util::do_sha(da_key + s)}; - std::memmove(&G.k[0], &a[0], G.k.SizeInBytes()); + std::memmove(G.k, a.c_str(), G.k_length); ++G.ctr; fmt::print("[i] generator: reseeded\n"); } catch(std::exception& e) { @@ -77,13 +77,11 @@ auto Generator::do_crypto() -> std::string { std::string plain{"Oh, I am fortune's fool!"}; std::string cipher, encoded_c; // in case we need to convert counter to string - // std::string str_ctr{reinterpret_cast(&G.k[0]), G.k.size()}; - // std::string str_ctr{(G.ctr)}; + std::string str_ctr{reinterpret_cast(&G.ctr)}; // 16 bytes --> 128bit static constexpr const std::size_t ctr_length{16}; CryptoPP::FixedSizeSecBlock ctr; - // FIXME: potential (pretty-much) UB - std::memmove(&ctr, &G.ctr, ctr_length); + std::memmove(ctr, str_ctr.c_str(), ctr_length); try { CTR_Mode::Encryption e; @@ -173,7 +171,7 @@ auto Generator::generate_random_data(uint n) -> std::string { /* clear out the old key and set a new one */ std::memset(G.k, 0x00, G.k.size()); - std::memmove(&G.k[0], &dst[0], dst.size()); + std::memmove(G.k, dst.c_str(), G.k_length); } catch(std::exception& e) { fmt::print("{}", e.what()); }