mirror of
https://github.com/postgres/postgres.git
synced 2025-06-26 12:21:12 +03:00
Fix misbehavior in contrib/pg_trgm with an unsatisfiable regex.
If the regex compiler can see that a regex is unsatisfiable (for example, '$foo') then it may emit an NFA having no arcs. pg_trgm's packGraph function did the wrong thing in this case; it would access off the end of a work array, and with bad luck could produce a corrupted output data structure causing more problems later. This could end with wrong answers or crashes in queries using a pg_trgm GIN or GiST index with such a regex. Fix by not trying to de-duplicate if there aren't at least 2 arcs. Per bug #17830 from Alexander Lakhin. Back-patch to all supported branches. Discussion: https://postgr.es/m/17830-57ff5f89bdb02b09@postgresql.org
This commit is contained in:
@ -1929,9 +1929,7 @@ packGraph(TrgmNFA *trgmNFA, MemoryContext rcontext)
|
||||
arcsCount;
|
||||
HASH_SEQ_STATUS scan_status;
|
||||
TrgmState *state;
|
||||
TrgmPackArcInfo *arcs,
|
||||
*p1,
|
||||
*p2;
|
||||
TrgmPackArcInfo *arcs;
|
||||
TrgmPackedArc *packedArcs;
|
||||
TrgmPackedGraph *result;
|
||||
int i,
|
||||
@ -2003,17 +2001,25 @@ packGraph(TrgmNFA *trgmNFA, MemoryContext rcontext)
|
||||
qsort(arcs, arcIndex, sizeof(TrgmPackArcInfo), packArcInfoCmp);
|
||||
|
||||
/* We could have duplicates because states were merged. Remove them. */
|
||||
/* p1 is probe point, p2 is last known non-duplicate. */
|
||||
p2 = arcs;
|
||||
for (p1 = arcs + 1; p1 < arcs + arcIndex; p1++)
|
||||
if (arcIndex > 1)
|
||||
{
|
||||
if (packArcInfoCmp(p1, p2) > 0)
|
||||
/* p1 is probe point, p2 is last known non-duplicate. */
|
||||
TrgmPackArcInfo *p1,
|
||||
*p2;
|
||||
|
||||
p2 = arcs;
|
||||
for (p1 = arcs + 1; p1 < arcs + arcIndex; p1++)
|
||||
{
|
||||
p2++;
|
||||
*p2 = *p1;
|
||||
if (packArcInfoCmp(p1, p2) > 0)
|
||||
{
|
||||
p2++;
|
||||
*p2 = *p1;
|
||||
}
|
||||
}
|
||||
arcsCount = (p2 - arcs) + 1;
|
||||
}
|
||||
arcsCount = (p2 - arcs) + 1;
|
||||
else
|
||||
arcsCount = arcIndex;
|
||||
|
||||
/* Create packed representation */
|
||||
result = (TrgmPackedGraph *)
|
||||
|
Reference in New Issue
Block a user