From 35d3f86aed27a4d4e3f4aa9e9372bd9cb3610a99 Mon Sep 17 00:00:00 2001
From: shihao zhong <zhong950419@gmail.com>
Date: Sat, 5 Dec 2026 00:33:13 -0500
Subject: [PATCH v1] Fix wraparound of phrase match positions in tsquery
 execution.

Clamp computed phrase positions to the limit, as tsvector_concat() does.

Reported-by: James Moore <james.moore@g5labs.ai>
Discussion: https://postgr.es/m/19960-29734b8086b69aec@postgresql.org
---
 src/backend/utils/adt/tsvector_op.c   | 10 ++++++++-
 src/test/regress/expected/tstypes.out | 31 +++++++++++++++++++++++++++
 src/test/regress/sql/tstypes.sql      |  7 ++++++
 3 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index bcb434c83e7..059e869c17b 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -1563,7 +1563,15 @@ TS_phrase_output(ExecPhraseData *data,
 					data->pos = palloc_array(WordEntryPos, max_npos);
 					data->allocated = true;
 				}
-				data->pos[data->npos++] = output_pos;
+
+				/*
+				 * Adding the offsets can push the position past what a
+				 * WordEntryPos can hold, and consumers only look at the low
+				 * 14 bits, so a larger value would wrap around and could
+				 * falsely match a small position.  Clamp it, as is done for
+				 * lexeme positions elsewhere.
+				 */
+				data->pos[data->npos++] = LIMITPOS(output_pos);
 			}
 			else
 			{
diff --git a/src/test/regress/expected/tstypes.out b/src/test/regress/expected/tstypes.out
index 4cfc3b9dc04..a7d8943affa 100644
--- a/src/test/regress/expected/tstypes.out
+++ b/src/test/regress/expected/tstypes.out
@@ -980,6 +980,37 @@ select to_tsvector('simple', '') @@ '!foo' AS "true";
  t
 (1 row)
 
+-- phrase positions must not wrap around past the position limit (16383)
+SELECT 'b:16383 c:1 d:4 e:2'::tsvector @@ '(b & !(c <2> d)) <-> e'::tsquery AS "false";
+ false 
+-------
+ f
+(1 row)
+
+SELECT 'a:16383 c:1'::tsvector @@ '(a <-> !b) <-> c'::tsquery AS "false";
+ false 
+-------
+ f
+(1 row)
+
+SELECT 'a:16383 c:1'::tsvector @@ '(a & !b) <2> c'::tsquery AS "false";
+ false 
+-------
+ f
+(1 row)
+
+SELECT 'a:16383 b:1 c:2 d:1'::tsvector @@ '(!(b <-> c) & a) <-> d'::tsquery AS "false";
+ false 
+-------
+ f
+(1 row)
+
+SELECT 'a:16382 c:16383'::tsvector @@ '(a & !b) <-> c'::tsquery AS "true";
+ true 
+------
+ t
+(1 row)
+
 --ranking
 SELECT ts_rank(' a:1 s:2C d g'::tsvector, 'a | s');
   ts_rank  
diff --git a/src/test/regress/sql/tstypes.sql b/src/test/regress/sql/tstypes.sql
index dfb7a1466e0..2085c85450a 100644
--- a/src/test/regress/sql/tstypes.sql
+++ b/src/test/regress/sql/tstypes.sql
@@ -188,6 +188,13 @@ select strip(to_tsvector('simple', 'x y q y')) @@ '!(x <2> y)' AS "true";
 select to_tsvector('simple', 'x y q y') @@ '!foo' AS "true";
 select to_tsvector('simple', '') @@ '!foo' AS "true";
 
+-- phrase positions must not wrap around past the position limit (16383)
+SELECT 'b:16383 c:1 d:4 e:2'::tsvector @@ '(b & !(c <2> d)) <-> e'::tsquery AS "false";
+SELECT 'a:16383 c:1'::tsvector @@ '(a <-> !b) <-> c'::tsquery AS "false";
+SELECT 'a:16383 c:1'::tsvector @@ '(a & !b) <2> c'::tsquery AS "false";
+SELECT 'a:16383 b:1 c:2 d:1'::tsvector @@ '(!(b <-> c) & a) <-> d'::tsquery AS "false";
+SELECT 'a:16382 c:16383'::tsvector @@ '(a & !b) <-> c'::tsquery AS "true";
+
 --ranking
 SELECT ts_rank(' a:1 s:2C d g'::tsvector, 'a | s');
 SELECT ts_rank(' a:1 sa:2C d g'::tsvector, 'a | s');
-- 
2.37.1 (Apple Git-137.1)

