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

Commitc3ccc9e

Browse files
committed
Fix possible buffer overrun in contrib/pg_trgm.
Allow for the possibility that folding a string to lower case makes itlonger (due to replacing a character with a longer multibyte character).This doesn't change the number of trigrams that will be extracted, butit does affect the required size of an intermediate buffer ingenerate_trgm(). Per bug #8821 from Ufuk Kayserilioglu.Also install some checks that the input string length is not so largeas to cause overflow in the calculations of palloc request sizes.Back-patch to all supported versions.
1 parent866a1f0 commitc3ccc9e

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

‎contrib/pg_trgm/trgm_op.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include"catalog/pg_type.h"
1111
#include"tsearch/ts_locale.h"
12+
#include"utils/memutils.h"
1213

1314

1415
PG_MODULE_MAGIC;
@@ -188,6 +189,18 @@ generate_trgm(char *str, int slen)
188189
char*bword,
189190
*eword;
190191

192+
/*
193+
* Guard against possible overflow in the palloc requests below. (We
194+
* don't worry about the additive constants, since palloc can detect
195+
* requests that are a little above MaxAllocSize --- we just need to
196+
* prevent integer overflow in the multiplications.)
197+
*/
198+
if ((Size) (slen /2) >= (MaxAllocSize / (sizeof(trgm)*3))||
199+
(Size)slen >= (MaxAllocSize /pg_database_encoding_max_length()))
200+
ereport(ERROR,
201+
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
202+
errmsg("out of memory")));
203+
191204
trg= (TRGM*)palloc(TRGMHDRSIZE+sizeof(trgm)* (slen /2+1)*3);
192205
trg->flag=ARRKEY;
193206
SET_VARSIZE(trg,TRGMHDRSIZE);
@@ -197,7 +210,8 @@ generate_trgm(char *str, int slen)
197210

198211
tptr=GETARR(trg);
199212

200-
buf=palloc(sizeof(char)* (slen+4));
213+
/* Allocate a buffer for case-folded, blank-padded words */
214+
buf= (char*)palloc(slen*pg_database_encoding_max_length()+4);
201215

202216
if (LPADDING>0)
203217
{
@@ -221,6 +235,7 @@ generate_trgm(char *str, int slen)
221235
#ifdefIGNORECASE
222236
pfree(bword);
223237
#endif
238+
224239
buf[LPADDING+bytelen]=' ';
225240
buf[LPADDING+bytelen+1]=' ';
226241

@@ -236,7 +251,10 @@ generate_trgm(char *str, int slen)
236251
if ((len=tptr-GETARR(trg))==0)
237252
returntrg;
238253

239-
if (len>0)
254+
/*
255+
* Make trigrams unique.
256+
*/
257+
if (len>1)
240258
{
241259
qsort((void*)GETARR(trg),len,sizeof(trgm),comp_trgm);
242260
len=unique_array(GETARR(trg),len);
@@ -419,6 +437,18 @@ generate_wildcard_trgm(const char *str, int slen)
419437
bytelen;
420438
constchar*eword;
421439

440+
/*
441+
* Guard against possible overflow in the palloc requests below. (We
442+
* don't worry about the additive constants, since palloc can detect
443+
* requests that are a little above MaxAllocSize --- we just need to
444+
* prevent integer overflow in the multiplications.)
445+
*/
446+
if ((Size) (slen /2) >= (MaxAllocSize / (sizeof(trgm)*3))||
447+
(Size)slen >= (MaxAllocSize /pg_database_encoding_max_length()))
448+
ereport(ERROR,
449+
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
450+
errmsg("out of memory")));
451+
422452
trg= (TRGM*)palloc(TRGMHDRSIZE+sizeof(trgm)* (slen /2+1)*3);
423453
trg->flag=ARRKEY;
424454
SET_VARSIZE(trg,TRGMHDRSIZE);
@@ -428,6 +458,7 @@ generate_wildcard_trgm(const char *str, int slen)
428458

429459
tptr=GETARR(trg);
430460

461+
/* Allocate a buffer for blank-padded, but not yet case-folded, words */
431462
buf=palloc(sizeof(char)* (slen+4));
432463

433464
/*
@@ -448,6 +479,7 @@ generate_wildcard_trgm(const char *str, int slen)
448479
* count trigrams
449480
*/
450481
tptr=make_trigrams(tptr,buf2,bytelen,charlen);
482+
451483
#ifdefIGNORECASE
452484
pfree(buf2);
453485
#endif
@@ -461,7 +493,7 @@ generate_wildcard_trgm(const char *str, int slen)
461493
/*
462494
* Make trigrams unique.
463495
*/
464-
if (len>0)
496+
if (len>1)
465497
{
466498
qsort((void*)GETARR(trg),len,sizeof(trgm),comp_trgm);
467499
len=unique_array(GETARR(trg),len);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp