8000 Fix misbehavior in contrib/pg_trgm with an unsatisfiable regex. · postgres/postgres@b183274 · GitHub
[go: up one dir, main page]

Skip to content

Commit b183274

Browse files
committed
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
1 parent 6e2674d commit b183274

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

contrib/pg_trgm/expected/pg_word_trgm.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,3 +1042,9 @@ select t,word_similarity('Kabankala',t) as sml from test_trgm2 where t %> 'Kaban
10421042
Waikala | 0.3
10431043
(89 rows)
10441044

1045+
-- test unsatisfiable pattern
1046+
select * from test_trgm2 where t ~ '.*$x';
1047+
t
1048+
---
1049+
(0 rows)
1050+

contrib/pg_trgm/sql/pg_word_trgm.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ select t,word_similarity('Baykal',t) as sml from test_trgm2 where 'Baykal' <% t
4040
select t,word_similarity('Kabankala',t) as sml from test_trgm2 where 'Kabankala' <% t order by sml desc, t;
4141
select t,word_similarity('Baykal',t) as sml from test_trgm2 where t %> 'Baykal' order by sml desc, t;
4242
select t,word_similarity('Kabankala',t) as sml from test_trgm2 where t %> 'Kabankala' order by sml desc, t;
43+
44+
-- test unsatisfiable pattern
45+
select * from test_trgm2 where t ~ '.*$x';

contrib/pg_trgm/trgm_regexp.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,9 +1935,7 @@ packGraph(TrgmNFA *trgmNFA, MemoryContext rcontext)
19351935
arcsCount;
19361936
HASH_SEQ_STATUS scan_status;
19371937
TrgmState *state;
1938-
TrgmPackArcInfo *arcs,
1939-
*p1,
1940-
*p2;
1938+
TrgmPackArcInfo *arcs;
19411939
TrgmPackedArc *packedArcs;
19421940
TrgmPackedGraph *result;
19431941
int i,
@@ -2009,17 +2007,25 @@ packGraph(TrgmNFA *trgmNFA, MemoryContext rcontext)
20092007
qsort(arcs, arcIndex, sizeof(TrgmPackArcInfo), packArcInfoCmp);
20102008

20112009
/* We could have duplicates because states were merged. Remove them. */
2012-
/* p1 is probe point, p2 is last known non-duplicate. */
2013-
p2 = arcs;
2014-
for (p1 = arcs + 1; p1 < arcs + arcIndex; p1++)
2010+
if (arcIndex > 1)
20152011
{
2016-
if (packArcInfoCmp(p1, p2) > 0)
2012+
/* p1 is probe point, p2 is last known non-duplicate. */
2013+
TrgmPackArcInfo *p1,
2014+
*p2;
2015+
2016+
p2 = arcs;
2017+
for (p1 = arcs + 1; p1 < arcs + arcIndex; p1++)
20172018
{
2018-
p2++;
2019-
*p2 = *p1;
2019+
if (packArcInfoCmp(p1, p2) > 0)
2020+
{
2021+
p2++;
2022+
*p2 = *p1;
2023+
}
20202024
}
2025+
arcsCount = (p2 - arcs) + 1;
20212026
}
2022-
arcsCount = (p2 - arcs) + 1;
2027+
else
2028+
arcsCount = arcIndex;
20232029

20242030
/* Create packed representation */
20252031
result = (TrgmPackedGraph *)

0 commit comments

Comments
 (0)
0