Fossil SCM

Resolve an obscure UB case involving unaligned read on a uint32_t pointer in sha1hard.c. Sugggestion, and link to related upstream patches, contributed by Dan Mestas.

stephan 2026-11-10 12:48 UTC trunk
Commit 9d5073e9d072f2edf6fa345d5782916236cee0668d00548a876da83bf97d470e
1 file changed +8 -1
+8 -1
--- src/sha1hard.c
+++ src/sha1hard.c
@@ -1359,11 +1359,18 @@
13591359
left = 0;
13601360
}
13611361
while (len >= 64)
13621362
{
13631363
ctx->total += 64;
1364
- sha1_process(ctx, (uint32_t*)(buf));
1364
+ if( (uintptr_t)buf & 3 ){
1365
+ /* A uint32_t load from a misaligned buf is undefined behavior.
1366
+ ** See notes in upstream sha1dc re. SHA1DC_ALLOW_UNALIGNED_ACCESS. */
1367
+ memcpy(ctx->buffer, buf, 64);
1368
+ sha1_process(ctx, (uint32_t*)(ctx->buffer));
1369
+ }else{
1370
+ sha1_process(ctx, (uint32_t*)(buf));
1371
+ }
13651372
buf += 64;
13661373
len -= 64;
13671374
}
13681375
if (len > 0)
13691376
{
13701377
--- src/sha1hard.c
+++ src/sha1hard.c
@@ -1359,11 +1359,18 @@
1359 left = 0;
1360 }
1361 while (len >= 64)
1362 {
1363 ctx->total += 64;
1364 sha1_process(ctx, (uint32_t*)(buf));
 
 
 
 
 
 
 
1365 buf += 64;
1366 len -= 64;
1367 }
1368 if (len > 0)
1369 {
1370
--- src/sha1hard.c
+++ src/sha1hard.c
@@ -1359,11 +1359,18 @@
1359 left = 0;
1360 }
1361 while (len >= 64)
1362 {
1363 ctx->total += 64;
1364 if( (uintptr_t)buf & 3 ){
1365 /* A uint32_t load from a misaligned buf is undefined behavior.
1366 ** See notes in upstream sha1dc re. SHA1DC_ALLOW_UNALIGNED_ACCESS. */
1367 memcpy(ctx->buffer, buf, 64);
1368 sha1_process(ctx, (uint32_t*)(ctx->buffer));
1369 }else{
1370 sha1_process(ctx, (uint32_t*)(buf));
1371 }
1372 buf += 64;
1373 len -= 64;
1374 }
1375 if (len > 0)
1376 {
1377

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button