Fossil SCM
checkin/384705b56ac5adb3e33ecbc5a4dd8e33393caed8b96f02afbb7799667afffe07
SHA1 hashes wrong on big-endian hosts
Symptom
On a big-endian host (reported on a PowerPC iBook), fossil update
in both the SQLite and Fossil repositories aborted with:
hash of rid 19096 does not match its uuid (
Abort trap
That message comes from verify_rid() in src/verify.c. Every artifact
gets checked against its stored hash before a commit, and this one failed.
Cause
The cause is check-in [d243deba6a] (2026-10-16), which updated
src/sha1hard.c to the current upstream sha1dc code.
SHA1 reads its input as big-endian 32-bit words. Little-endian hosts
have to byte-swap each word, and big-endian hosts must not. The previous
sha1hard.c checked the host byte order at runtime (ctx->bigendian)
and skipped the swap on big-endian hosts. The updated code makes that
choice at compile time instead:
#if defined(BIGENDIAN)
#define sha1_load(m, t) (m[t])
#else
#define sha1_load(m, t, temp) { temp = m[t]; sha1_bswap32(temp); }
#endif
That code has two defects:
- Nothing in the build defines
BIGENDIAN, so every platform takes the byte-swapping branch. Upstream sha1dc decides this with a large block ofSHA1DC_BIGENDIANdetection, which didn't come over in the update. - The big-endian branch couldn't have compiled anyway. Its macro takes two arguments, but its only caller passes three.
The result is that every SHA1 hash computed on a big-endian host is
wrong. The first SHA1-named artifact that gets checked fails in
verify_rid(). SHA3 hashing doesn't use this code and isn't affected.
That's why the failure shows up as a hash mismatch on older artifacts
instead of some more general breakage.
Fix
All of the changes are in src/sha1hard.c:
- Add a condensed version of upstream's byte-order detection. It uses
__BYTE_ORDER__/__ORDER_BIG_ENDIAN__when the compiler provides them. Otherwise it falls back to well-known big-endian architecture macros (__BIG_ENDIAN__,__ppc__,__powerpc__,__ARMEB__, the MIPS-EB family, SPARC, HPPA, s390 and m68k). The fallback matters for old compilers such as Apple's PowerPC gcc, which may not define__BYTE_ORDER__. DefiningSHA1DC_BIGENDIANorSHA1DC_LITTLEENDIANat build time overrides the detection. - Fix the big-endian
sha1_load()macro so that it takes the same three arguments as its little-endian counterpart. - Add a runtime check to
SHA1DCInit()that compares the compile-time answer with the actual byte order. If they disagree, Fossil prints an error and aborts. A wrong guess on some platform we haven't thought of then fails loudly instead of silently producing wrong hashes.
Testing Performed
- On x86_64 Linux,
fossil sha1sumoutput matches the systemsha1sum. - On x86_64 Linux,
fossil test-integrityon the Fossil repository checked 68649 blobs with 0 errors. - The big-endian code path compiles cleanly when forced with
-DSHA1DC_BIGENDIAN. - Confirmed on PowerPC iBook by drh.
Note for affected installations
A big-endian build from between [d243deba6a] and this fix computes
wrong SHA1 hashes. Because the check before each commit aborts the
operation, it's unlikely that bad data was stored. Even so, it would
be worth running fossil test-integrity with a fixed build on any
repositories that such a binary used.