Skip to content

Commit 5f6a112

Browse files
jrngitster
authored andcommitted
block-sha1: avoid pointer conversion that violates alignment constraints
With 660231a (block-sha1: support for architectures with memory alignment restrictions, 2009-08-12), blk_SHA1_Update was modified to access 32-bit chunks of memory one byte at a time on arches that prefer that: #define get_be32(p) ( \ (*((unsigned char *)(p) + 0) << 24) | \ (*((unsigned char *)(p) + 1) << 16) | \ (*((unsigned char *)(p) + 2) << 8) | \ (*((unsigned char *)(p) + 3) << 0) ) The code previously accessed these values by just using htonl(*p). Unfortunately, Michael noticed on an Alpha machine that git was using plain 32-bit reads anyway. As soon as we convert a pointer to int *, the compiler can assume that the object pointed to is correctly aligned as an int (C99 section 6.3.2.3 "pointer conversions" paragraph 7), and gcc takes full advantage by using a single 32-bit load, resulting in a whole bunch of unaligned access traps. So we need to obey the alignment constraints even when only dealing with pointers instead of actual values. Do so by changing the type of 'data' to void *. This patch renames 'data' to 'block' at the same time to make sure all references are updated to reflect the new type. Reported-tested-and-explained-by: Michael Cree <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Acked-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 30ae47b commit 5f6a112

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

block-sha1/sha1.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
* Where do we get the source from? The first 16 iterations get it from
101101
* the input data, the next mix it from the 512-bit array.
102102
*/
103-
#define SHA_SRC(t) get_be32(data + t)
103+
#define SHA_SRC(t) get_be32((unsigned char *) block + t*4)
104104
#define SHA_MIX(t) SHA_ROL(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1)
105105

106106
#define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
@@ -114,7 +114,7 @@
114114
#define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B&C)+(D&(B^C))) , 0x8f1bbcdc, A, B, C, D, E )
115115
#define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
116116

117-
static void blk_SHA1_Block(blk_SHA_CTX *ctx, const unsigned int *data)
117+
static void blk_SHA1_Block(blk_SHA_CTX *ctx, const void *block)
118118
{
119119
unsigned int A,B,C,D,E;
120120
unsigned int array[16];
@@ -125,7 +125,7 @@ static void blk_SHA1_Block(blk_SHA_CTX *ctx, const unsigned int *data)
125125
D = ctx->H[3];
126126
E = ctx->H[4];
127127

128-
/* Round 1 - iterations 0-16 take their input from 'data' */
128+
/* Round 1 - iterations 0-16 take their input from 'block' */
129129
T_0_15( 0, A, B, C, D, E);
130130
T_0_15( 1, E, A, B, C, D);
131131
T_0_15( 2, D, E, A, B, C);

0 commit comments

Comments
 (0)