>From f108257a300de2435fc9b625c15b9e9ea0abced2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 15 Sep 2015 23:55:00 +0200 Subject: [PATCH 2/3] Avoid g++ 5 warnings about breaking strict aliasing Using reinterpret_cast<> results in -Wstrict-aliasing warnings from g++ 5.2, use std::memcpy() explicitly to avoid them. --- actuarial_table.cpp | 20 +++++++++++++------- md5.cpp | 7 ++++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/actuarial_table.cpp b/actuarial_table.cpp index 680d053..1eb4d10 100644 --- a/actuarial_table.cpp +++ b/actuarial_table.cpp @@ -48,6 +48,7 @@ #include // std::max(), std::min() #include // std::toupper() #include // CHAR_BIT +#include // std::memcpy() #include // std::setprecision() #include #include @@ -84,7 +85,7 @@ t = invalid; char z[sizeof(T)]; is.read(z, sizeof(T)); - t = *reinterpret_cast(z); + std::memcpy(&t, z, sizeof(T)); LMI_ASSERT(invalid != t); return t; } @@ -573,13 +574,16 @@ void soa_actuarial_table::find_table() BOOST_STATIC_ASSERT(sizeof(boost::int32_t) <= sizeof(int)); while(index_ifs) { - int index_table_number = - *reinterpret_cast(index_record) - ; + boost::int32_t index_table_number; + std::memcpy + (&index_table_number + ,index_record + ,sizeof(index_table_number) + ); if(table_number_ == index_table_number) { - char* p = 54 + index_record; - boost::int32_t z = *reinterpret_cast(p); + boost::int32_t z; + std::memcpy(&z, 54 + index_record, sizeof(z)); table_offset_ = std::streampos(static_cast(z)); break; } @@ -810,7 +814,9 @@ void soa_actuarial_table::read_values(std::istream& is, int nominal_length) for(int j = 0; j < number_of_values; ++j) { is.read(z, sizeof(double)); - data_[j] = *reinterpret_cast(z); + double d; + std::memcpy(&d, z, sizeof(double)); + data_[j] = d; } } diff --git a/md5.cpp b/md5.cpp index b1c651a..df838b6 100644 --- a/md5.cpp +++ b/md5.cpp @@ -161,9 +161,10 @@ md5_finish_ctx (struct md5_ctx* ctx, void* resbuf) std::memcpy (&ctx->buffer[bytes], fillbuf, pad); /* Put the 64-bit file length in *bits* at the end of the buffer. */ - *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); - *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | - (ctx->total[0] >> 29)); + md5_uint32 z = SWAP (ctx->total[0] << 3); + std::memcpy(&ctx->buffer[bytes + pad], &z, sizeof(z)); + z = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); + std::memcpy(&ctx->buffer[bytes + pad + 4], &z, sizeof(z)); /* Process last bytes. */ md5_process_block (ctx->buffer, bytes + pad + 8, ctx); -- 2.1.0