>From 55adecbb1098c786875e381a1a3228e56def6a77 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 20 Sep 2015 00:04:29 +0200 Subject: [PATCH 1/2] Avoid breaking strict aliasing rules in MD5 code Creating a pointer of a different type to the same memory location, as the existing code did, is not allowed by the language rules and resulted in undefined behaviour and warnings from g++ with -O2 or higher. Simply use std::memcpy() instead to avoid both problems, at the cost of making the code slightly more uglier. --- md5.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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.5.1