Skip to content

Commit

Permalink
digest: fix aliasing bug with gcc 11
Browse files Browse the repository at this point in the history
gcc 11 with -O2 optimizes away the store of the bit length into the
last 8 bytes of the context buffer due to an aliasing violation
(stored through uint64_t, retrieved through uint32_t).

To fix this, import the NetBSD patch from christos[0] which makes
it use memcpy instead.

[0] http://cvsweb.netbsd.org/bsdweb.cgi/src/common/lib/libc/hash/sha2/sha2.c.diff?r1=1.12&r2=1.13
  • Loading branch information
michaelforney committed Feb 15, 2022
1 parent 0ab885e commit b631703
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pkgtools/digest/files/configure
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,8 @@ MAKEFLAGS=
# Identity of this package.
PACKAGE_NAME='nbsd-digest'
PACKAGE_TARNAME='nbsd-digest'
PACKAGE_VERSION='20211023'
PACKAGE_STRING='nbsd-digest 20190127'
PACKAGE_VERSION='20220214'
PACKAGE_STRING='nbsd-digest 20220214'
PACKAGE_BUGREPORT='[email protected]'
PACKAGE_URL=''

Expand Down
3 changes: 2 additions & 1 deletion pkgtools/digest/files/sha2.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,8 @@ void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
*context->buffer = 0x80;
}
/* Set the bit count: */
*(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
MEMCPY_BCOPY(&context->buffer[SHA256_SHORT_BLOCK_LENGTH],
&context->bitcount, sizeof(context->bitcount));

/* Final transform: */
SHA256_Transform(context, (sha2_word32*)context->buffer);
Expand Down

0 comments on commit b631703

Please sign in to comment.