postgres.git / summary / log / commit / refs

commit    2d7573b0098d035db01a742bb1475d4a7f45ff57
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Commit:   Noah Misch <noah@leadboat.com>
Date:     Mon Nov 09 14:20:50 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0

    Fix potential buffer overrun in ts_rank_cd().
    
    get_docrep() assumed there could be no more QueryItems per group
    than there are lexemes in the input tsvector.  That's true with valid
    tsvector input, which will never show duplicate positions for any
    lexeme.  But with duplicate positions it's possible to overrun the
    allocated space.  Guard against that by dropping items if there are
    more than we expect (it doesn't seem worth working harder than that).
    
    The submitted test case that reached this constructed an invalid
    tsvector by dint of starting with one, namely a tsvector containing
    a zero position, which tsvectorrecv() failed to prevent before
    10cc5aa96.  It's not clear whether the problem is reachable without
    that.
    
    Reported-by: Anthropic OSS program
    Author: Anthropic OSS program
    Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
    Backpatch-through: 14
    Security: CVE-2026-116317


src/backend/utils/adt/tsrank.c | 14 ++++++++++++-- src/include/tsearch/ts_type.h | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/backend/utils/adt/tsrank.c b/src/backend/utils/adt/tsrank.c index e5b850ea8ef..00856147f06 100644 --- a/src/backend/utils/adt/tsrank.c +++ b/src/backend/utils/adt/tsrank.c @@ -819,8 +819,18 @@ get_docrep(TSVector txt, QueryRepresentation *qr, int *doclen) if (rptr->pos == (rptr - 1)->pos && rptr->data.map.entry == (rptr - 1)->data.map.entry) { - storage.data.query.items[storage.data.query.nitem] = rptr->data.map.item; - storage.data.query.nitem++; + /* + * A valid tsvector never lists the same position twice for a + * lexeme, so a group can't have more than one item per query + * operand. But with corrupt data we might see too many + * entries; if so, drop entries rather than overrunning + * items[]. + */ + if (storage.data.query.nitem < qr->query->size) + { + storage.data.query.items[storage.data.query.nitem] = rptr->data.map.item; + storage.data.query.nitem++; + } } else { diff --git a/src/include/tsearch/ts_type.h b/src/include/tsearch/ts_type.h index 5bfae27e32e..6a314bf31ef 100644 --- a/src/include/tsearch/ts_type.h +++ b/src/include/tsearch/ts_type.h @@ -33,7 +33,7 @@ * uint16 number of positions that follow * WordEntryPos[] positions * - * The positions for each lexeme must be sorted. + * The positions for each lexeme must be nonzero, sorted, and unique. * * Note that while the WordEntry items must be sorted per tsCompareString(), * the per-lexeme data storage could be in some other order, ie the series [parent: 834255ca8580]