BUG #19920: Out-of-Bounds Read on a Zero-Length Array in pg_trgm word_similarity('', '')
The following bug has been logged on the website:
Bug reference: 19920
Logged by: Fin
Email address: nakamurajames123@gmail.com
PostgreSQL version: 19rc2
Operating system: wsl linux
Description:
Reproduction Steps
The issue can be reproduced with the default server configuration.
Since pg_trgm is a trusted extension, any regular user with CREATE privilege
on the database can install it.
CREATE EXTENSION pg_trgm;
SELECT word_similarity('', '');
The same code path is triggered by other inputs where neither string
produces any trigrams, such as:
SELECT strict_word_similarity('!', '?');
SELECT '' <<-> '';
Expected Behavior
When neither input string produces any trigrams, the function should perform
all calculations within allocated memory bounds and consistently return the
same value (0).
Actual Behavior
In calc_word_similarity(), an array is allocated with zero elements, but the
function reads found[0], accessing one byte outside the allocated array
bounds.
The value of this byte determines whether ulen1 is incremented, meaning the
result may depend on residual memory contents.
- In a normal release build, the out-of-bounds read occurs silently without
an error.
- In a build where PostgreSQL's Valgrind memory-context annotations
(USE_VALGRIND) are connected to AddressSanitizer, the invalid access is
detected as a use-after-poison, as shown below.
- Both the master branch (PostgreSQL 20devel) and PostgreSQL 19.0 are
affected.
Logs
1. psql output, master (PostgreSQL 20devel, 0cf2d43d326).
In the ASan-enabled build, the backend aborts when the invalid memory access
is detected.
SELECT word_similarity('', '');
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
2. Server log, master (ASan report; source path prefixes have been
shortened):
==3179484==ERROR: AddressSanitizer: use-after-poison on address
0x7aed964b7200 at pc 0x7c9d967e2b2e bp 0x7ffe374c3850 sp 0x7ffe374c3848
READ of size 1 at 0x7aed964b7200 thread T0
#0 calc_word_similarity contrib/pg_trgm/trgm_op.c:922:6
#1 word_similarity contrib/pg_trgm/trgm_op.c:1367:8
#2 ExecInterpExpr
src/backend/executor/execExprInterp.c:1011:9
#3 ExecEvalExprSwitchContext
src/include/executor/executor.h:452:13
#4 evaluate_expr
src/backend/optimizer/util/clauses.c:5979:14
#5 evaluate_function
src/backend/optimizer/util/clauses.c:5486:9
#6 simplify_function
src/backend/optimizer/util/clauses.c:4753:12
#7 eval_const_expressions_mutator
src/backend/optimizer/util/clauses.c:2984:14
...
#13 preprocess_expression
src/backend/optimizer/plan/planner.c:1448:10
src/backend/optimizer/plan/planner.c:543:9
#17 pg_plan_query src/backend/tcop/postgres.c:923:9
#19 exec_simple_query src/backend/tcop/postgres.c:1221:19
#20 PostgresMain src/backend/tcop/postgres.c
0x7aed964b7200 is located 256 bytes inside of 8192-byte region
[0x7aed964b7100,0x7aed964b9100)
allocated by thread T0 here:
#0 malloc
#1 AllocSetContextCreateInternal src/backend/utils/mmgr/aset.c:492:19
#2 CreateExprContextInternal
src/backend/executor/execUtils.c:264:3
...
SUMMARY: AddressSanitizer: use-after-poison contrib/pg_trgm/trgm_op.c:922:6
in calc_word_similarity
LOG: client backend (PID 3179484) was terminated by signal 6: Aborted
DETAIL: Failed process was running: SELECT word_similarity('', '');
3. PostgreSQL 19.0 (REL_19_STABLE, 19aa5713be2).
The same issue is detected at the same source location.
READ of size 1 at 0x76aa84cb7200 thread T0
#0 calc_word_similarity contrib/pg_trgm/trgm_op.c:922:6
#1 word_similarity contrib/pg_trgm/trgm_op.c:1367:8
SUMMARY: AddressSanitizer: use-after-poison contrib/pg_trgm/trgm_op.c:922:6
in calc_word_similarity
On 19 Nov 2026, at 02:33, PG Bug reporting form <noreply@postgresql.org> wrote:
In calc_word_similarity(), an array is allocated with zero elements, but the
function reads found[0], accessing one byte outside the allocated array
bounds.
Verified, the trivial diff attached avoids this.
--
Daniel Gustafsson
On Thu, 19 Nov 2026, 16:53 Daniel Gustafsson, <daniel@yesql.se> wrote:
On 19 Nov 2026, at 02:33, PG Bug reporting form <noreply@postgresql.org>
wrote:
In calc_word_similarity(), an array is allocated with zero elements, but
the
function reads found[0], accessing one byte outside the allocated array
bounds.Verified, the trivial diff attached avoids this.
--
Daniel Gustafsson
I have also found this some time ago with UBSAN, but didn't report due to
no actual server crash. Anyway LGTM
Show quoted text
On 19 Nov 2026, at 13:10, Kirill Reshke <reshkekirill@gmail.com> wrote:
I have also found this some time ago with UBSAN, but didn't report due to no actual server crash. Anyway LGTM
Thanks for confirming. We could of course try and exit early on zero length
but it hardly seems worth the trouble to optimize a nonsensical case, thus the
trivial patch.
--
Daniel Gustafsson