changeset 10128:92a80b637ea1

avoid unaligned access errors, e.g., on sparc * lib/sha512.c (sha512_conclude_ctx): Use set_uint64 rather than direct access through a possibly-unaligned uint64* pointer. * lib/sha256.c (sha256_conclude_ctx): Use set_uint32 rather than direct access through a possibly-unaligned uint32* pointer. Prompted by this patch from Tom "spot" Callaway: http://thread.gmane.org/gmane.comp.gnu.coreutils.bugs/13638
author Jim Meyering <meyering@redhat.com>
date Mon, 26 May 2008 16:25:28 +0200
parents fc6baf7d0ac0
children 4c9de4cfa188
files ChangeLog lib/sha256.c lib/sha512.c
diffstat 3 files changed, 23 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2008-05-26  Jim Meyering  <meyering@redhat.com>
 
+	avoid unaligned access errors, e.g., on sparc
+	* lib/sha512.c (sha512_conclude_ctx): Use set_uint64 rather than
+	direct access through a possibly-unaligned uint64* pointer.
+	* lib/sha256.c (sha256_conclude_ctx): Use set_uint32 rather than
+	direct access through a possibly-unaligned uint32* pointer.
+	Prompted by this patch from Tom "spot" Callaway:
+	http://thread.gmane.org/gmane.comp.gnu.coreutils.bugs/13638
+
 	sha512.c: fix typo in comment
 	* lib/sha512.c (sha512_conclude_ctx): Length is 128-bit, not 64-bit.
 
--- a/lib/sha256.c
+++ b/lib/sha256.c
@@ -134,9 +134,13 @@
   if (ctx->total[0] < bytes)
     ++ctx->total[1];
 
-  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
-  ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
-  ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
+  /* Put the 64-bit file length in *bits* at the end of the buffer.
+     Use set_uint32 rather than a simple assignment, to avoid risk of
+     unaligned access.  */
+  set_uint32 ((char *) &ctx->buffer[size - 2],
+	      SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)));
+  set_uint32 ((char *) &ctx->buffer[size - 1],
+	      SWAP (ctx->total[0] << 3));
 
   memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
 
--- a/lib/sha512.c
+++ b/lib/sha512.c
@@ -141,10 +141,14 @@
   if (u64lt (ctx->total[0], u64lo (bytes)))
     ctx->total[1] = u64plus (ctx->total[1], u64lo (1));
 
-  /* Put the 128-bit file length in *bits* at the end of the buffer.  */
-  ctx->buffer[size - 2] = SWAP (u64or (u64shl (ctx->total[1], 3),
-				       u64shr (ctx->total[0], 61)));
-  ctx->buffer[size - 1] = SWAP (u64shl (ctx->total[0], 3));
+  /* Put the 128-bit file length in *bits* at the end of the buffer.
+     Use set_uint64 rather than a simple assignment, to avoid risk of
+     unaligned access.  */
+  set_uint64 ((char *) &ctx->buffer[size - 2],
+	      SWAP (u64or (u64shl (ctx->total[1], 3),
+			   u64shr (ctx->total[0], 61))));
+  set_uint64 ((char *) &ctx->buffer[size - 1],
+	      SWAP (u64shl (ctx->total[0], 3)));
 
   memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 8 - bytes);