`sha256()` returns a wrong hash when libsql-ffi is built with gcc 11 or later

Open Beginner friendly
#2,333 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 11 days

@colinhacks is already working on this.

Since Nov 19, 2026.

  • #2334 by @colinhacks — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, sqlite
Domain
databases

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tursodatabase/libsql

All issues in tursodatabase/libsql

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.