Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbc436e4

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 luckcould produce a corrupted output data structure causing moreproblems later. This could end with wrong answers or crashesin 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 supportedbranches.Discussion:https://postgr.es/m/17830-57ff5f89bdb02b09@postgresql.org
1 parent866fd00 commitbc436e4

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
@@ -1044,3 +1044,9 @@ select t,word_similarity('Kabankala',t) as sml from test_trgm2 where t %> 'Kaban
10441044
Waikala | 0.3
10451045
(89 rows)
10461046

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

‎contrib/pg_trgm/sql/pg_word_trgm.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ select t,word_similarity('Baykal',t) as sml from test_trgm2 where 'Baykal' <% t
4343
select t,word_similarity('Kabankala',t)as smlfrom test_trgm2where'Kabankala'<% torder by smldesc, t;
4444
select t,word_similarity('Baykal',t)as smlfrom test_trgm2where t %>'Baykal'order by smldesc, t;
4545
select t,word_similarity('Kabankala',t)as smlfrom test_trgm2where t %>'Kabankala'order by smldesc, t;
46+
47+
-- test unsatisfiable pattern
48+
select*from test_trgm2where t ~'.*$x';

‎contrib/pg_trgm/trgm_regexp.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,9 +1929,7 @@ packGraph(TrgmNFA *trgmNFA, MemoryContext rcontext)
19291929
arcsCount;
19301930
HASH_SEQ_STATUSscan_status;
19311931
TrgmState*state;
1932-
TrgmPackArcInfo*arcs,
1933-
*p1,
1934-
*p2;
1932+
TrgmPackArcInfo*arcs;
19351933
TrgmPackedArc*packedArcs;
19361934
TrgmPackedGraph*result;
19371935
inti,
@@ -2003,17 +2001,25 @@ packGraph(TrgmNFA *trgmNFA, MemoryContext rcontext)
20032001
qsort(arcs,arcIndex,sizeof(TrgmPackArcInfo),packArcInfoCmp);
20042002

20052003
/* We could have duplicates because states were merged. Remove them. */
2006-
/* p1 is probe point, p2 is last known non-duplicate. */
2007-
p2=arcs;
2008-
for (p1=arcs+1;p1<arcs+arcIndex;p1++)
2004+
if (arcIndex>1)
20092005
{
2010-
if (packArcInfoCmp(p1,p2)>0)
2006+
/* p1 is probe point, p2 is last known non-duplicate. */
2007+
TrgmPackArcInfo*p1,
2008+
*p2;
2009+
2010+
p2=arcs;
2011+
for (p1=arcs+1;p1<arcs+arcIndex;p1++)
20112012
{
2012-
p2++;
2013-
*p2=*p1;
2013+
if (packArcInfoCmp(p1,p2)>0)
2014+
{
2015+
p2++;
2016+
*p2=*p1;
2017+
}
20142018
}
2019+
arcsCount= (p2-arcs)+1;
20152020
}
2016-
arcsCount= (p2-arcs)+1;
2021+
else
2022+
arcsCount=arcIndex;
20172023

20182024
/* Create packed representation */
20192025
result= (TrgmPackedGraph*)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp