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.
Commit
9d5073e9d072f2edf6fa345d5782916236cee0668d00548a876da83bf97d470e
Parent
9b0c207621de910…
1 file changed
+8
-1
+8
-1
| --- src/sha1hard.c | ||
| +++ src/sha1hard.c | ||
| @@ -1359,11 +1359,18 @@ | ||
| 1359 | 1359 | left = 0; |
| 1360 | 1360 | } |
| 1361 | 1361 | while (len >= 64) |
| 1362 | 1362 | { |
| 1363 | 1363 | 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 | + } | |
| 1365 | 1372 | buf += 64; |
| 1366 | 1373 | len -= 64; |
| 1367 | 1374 | } |
| 1368 | 1375 | if (len > 0) |
| 1369 | 1376 | { |
| 1370 | 1377 |
| --- 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 |