9
9
10
10
#include "catalog/pg_type.h"
11
11
#include "tsearch/ts_locale.h"
12
+ #include "utils/memutils.h"
12
13
#include "utils/array.h"
13
14
14
15
@@ -192,6 +193,18 @@ generate_trgm(char *str, int slen)
192
193
char * bword ,
193
194
* eword ;
194
195
196
+ /*
197
+ * Guard against possible overflow in the palloc requests below. (We
198
+ * don't worry about the additive constants, since palloc can detect
199
+ * requests that are a little above MaxAllocSize --- we just need to
200
+ * prevent integer overflow in the multiplications.)
201
+ */
202
+ if ((Size ) (slen /2 ) >= (MaxAllocSize / (sizeof (trgm )* 3 ))||
203
+ (Size )slen >= (MaxAllocSize /pg_database_encoding_max_length ()))
204
+ ereport (ERROR ,
205
+ (errcode (ERRCODE_PROGRAM_LIMIT_EXCEEDED ),
206
+ errmsg ("out of memory" )));
207
+
195
208
trg = (TRGM * )palloc (TRGMHDRSIZE + sizeof (trgm )* (slen /2 + 1 )* 3 );
196
209
trg -> flag = ARRKEY ;
197
210
SET_VARSIZE (trg ,TRGMHDRSIZE );
@@ -201,7 +214,8 @@ generate_trgm(char *str, int slen)
201
214
202
215
tptr = GETARR (trg );
203
216
204
- buf = palloc (sizeof (char )* (slen + 4 ));
217
+ /* Allocate a buffer for case-folded, blank-padded words */
218
+ buf = (char * )palloc (slen * pg_database_encoding_max_length ()+ 4 );
205
219
206
220
if (LPADDING > 0 )
207
221
{
@@ -225,6 +239,7 @@ generate_trgm(char *str, int slen)
225
239
#ifdef IGNORECASE
226
240
pfree (bword );
227
241
#endif
242
+
228
243
buf [LPADDING + bytelen ]= ' ' ;
229
244
buf [LPADDING + bytelen + 1 ]= ' ' ;
230
245
@@ -240,7 +255,10 @@ generate_trgm(char *str, int slen)
240
255
if ((len = tptr - GETARR (trg ))== 0 )
241
256
return trg ;
242
257
243
- if (len > 0 )
258
+ /*
259
+ * Make trigrams unique.
260
+ */
261
+ if (len > 1 )
244
262
{
245
263
qsort ((void * )GETARR (trg ),len ,sizeof (trgm ),comp_trgm );
246
264
len = unique_array (GETARR (trg ),len );
@@ -423,6 +441,18 @@ generate_wildcard_trgm(const char *str, int slen)
423
441
bytelen ;
424
442
const char * eword ;
425
443
444
+ /*
445
+ * Guard against possible overflow in the palloc requests below. (We
446
+ * don't worry about the additive constants, since palloc can detect
447
+ * requests that are a little above MaxAllocSize --- we just need to
448
+ * prevent integer overflow in the multiplications.)
449
+ */
450
+ if ((Size ) (slen /2 ) >= (MaxAllocSize / (sizeof (trgm )* 3 ))||
451
+ (Size )slen >= (MaxAllocSize /pg_database_encoding_max_length ()))
452
+ ereport (ERROR ,
453
+ (errcode (ERRCODE_PROGRAM_LIMIT_EXCEEDED ),
454
+ errmsg ("out of memory" )));
455
+
426
456
trg = (TRGM * )palloc (TRGMHDRSIZE + sizeof (trgm )* (slen /2 + 1 )* 3 );
427
457
trg -> flag = ARRKEY ;
428
458
SET_VARSIZE (trg ,TRGMHDRSIZE );
@@ -432,6 +462,7 @@ generate_wildcard_trgm(const char *str, int slen)
432
462
433
463
tptr = GETARR (trg );
434
464
465
+ /* Allocate a buffer for blank-padded, but not yet case-folded, words */
435
466
buf = palloc (sizeof (char )* (slen + 4 ));
436
467
437
468
/*
@@ -452,6 +483,7 @@ generate_wildcard_trgm(const char *str, int slen)
452
483
* count trigrams
453
484
*/
454
485
tptr = make_trigrams (tptr ,buf2 ,bytelen ,charlen );
486
+
455
487
#ifdef IGNORECASE
456
488
pfree (buf2 );
457
489
#endif
@@ -465,7 +497,7 @@ generate_wildcard_trgm(const char *str, int slen)
465
497
/*
466
498
* Make trigrams unique.
467
499
*/
468
- if (len > 0 )
500
+ if (len > 1 )
469
501
{
470
502
qsort ((void * )GETARR (trg ),len ,sizeof (trgm ),comp_trgm );
471
503
len = unique_array (GETARR (trg ),len );