`sha256()` returns a wrong hash when libsql-ffi is built with gcc 11 or later
Maintainers usually reply within 11 days
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
Start with libsql-ffi/bundled/sqlean/crypto/sha2.c and inspect sha256_final, where the issue identifies a strict-aliasing write of the message length. Build the bundled code with gcc 11 or later using the flags described in the issue, then run select hex(sha256('a')); to confirm the output. Done means the query returns the standard SHA-256 hash shown in the issue under those compiler settings.
Written by the indexing model from the issue text.
Description
With the sqlean crypto extension (sqlean-extension-crypto, which libsql-server turns on through sqlean-extensions), sha256() returns the wrong hash when the C code is built with gcc 11 or later:
select hex(sha256('a'));
-- 767DD50DA1B5B88651E238AA239F5FE9F934BEA61F909E70785EED84FD2331FC gcc 11 to 16, -O2 or -O3
-- CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB gcc 10, clang, or gcc with -fno-strict-aliasing
That's from libsql-ffi/bundled/src/sqlite3.c and libsql-ffi/bundled/sqlean/crypto/*.c on main, compiled with -O3 -DSQLITE_CORE as build.rs does, calling crypto_init and running the query. The bundled sha2.c alone shows the same thing with gcc 11, 12, 13, 14, 15 and 16. sha512() was right with gcc 16 and clang.
The cause is the write of the message length in sha256_final, a strict-aliasing violation that gcc 11 and later reorder:
*(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
A memcpy there gives the right hash with gcc 16 at -O2 and -O3:
memcpy(&context->buffer[SHA256_SHORT_BLOCK_LENGTH], &context->bitcount, sizeof(context->bitcount));
The sqld image on ghcr.io (0.24.33) returns the right hash today because it builds on rust:slim-bullseye, whose gcc is 10. A release build of sqld from source on a current distro, or moving the image to bookworm (gcc 12), gives the wrong one. Upstream sqlean has since replaced this sha2.c (nalgeon/sqlean@db85bbb, and again in October 2026), so updating the bundled crypto would also fix it.
- Dominant language
- C
- Stars
- 17.3k
- Forks
- 562
- Avg merge
- 10d 15h
- Merged PRs (30d)
- 1
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tursodatabase/libsql
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
tursodatabase/libsql#2320 ·
Maintainers usually reply within 11 days
-
`cloudflare` feature: hrana streams are never closedPossibly taken @pcbeingused333 claimed this 7 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
tursodatabase/libsql#2312 ·
Maintainers usually reply within 11 days
-
bottomless: finished S3 upload tasks are never reaped, so sqld memory grows by ~10 KB per WAL batchPossibly taken @pavanmanishd claimed this 27 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
tursodatabase/libsql#2298 · 1 comment ·
Maintainers usually reply within 11 days
-
Rust local Row::get_value truncates embedded NUL TEXT and panics on malformed UTF-8 (0.9.30)Possibly taken @Sanjays2402 claimed this 38 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
tursodatabase/libsql#2285 ·
Maintainers usually reply within 11 days
-
libsql-ffi: broken builds under Nix on macOSPossibly taken @frectonz claimed this 88 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
tursodatabase/libsql#2275 ·
Maintainers usually reply within 11 days
All issues in tursodatabase/libsql
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
OpenPrinting/cups#1826 ·
Maintainers usually reply within 1 day
-
Backport to 3.0 advertises a suffix that does not existPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
389ds/389-ds-base#8152 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
RetroAchievements/rcheevos#580 · 1 comment ·
Maintainers usually reply within 5 days
-
meta:feature-request
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
mpv-player/mpv#18855 · 2 comments ·
Maintainers usually reply within 1 day