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

Commit9fe285f

Browse files
committed
Preserve memory context of VarStringSortSupport buffers.
When enlarging the work buffers of a VarStringSortSupport object,varstrfastcmp_locale was careful to keep them in the ssup_cxtmemory context; but varstr_abbrev_convert just used palloc().The latter creates a hazard that the buffers could be freed outfrom under the VarStringSortSupport object, resulting in stompingon whatever gets allocated in that memory later.In practice, because we only use this code for ICU collations(cf.3df9c37), the problem is confined to use of ICU collations.I believe it may have been unreachable before the introductionof incremental sort, too, as traditional sorting usually justuses one context for the duration of the sort.We could fix this by making the broken stanzas in varstr_abbrev_convertmatch the non-broken ones in varstrfastcmp_locale. However, it seemslike a better idea to dodge the issue altogether by replacing thepfree-and-allocate-anew coding with repalloc, which automaticallypreserves the chunk's memory context. This fix does add a few cyclesbecause repalloc will copy the chunk's content, which the existingcoding assumes is useless. However, we don't expect that these bufferenlargement operations are performance-critical. Besides that, it'sfar from obvious that copying the buffer contents isn't required, sincethese stanzas make no effort to mark the buffers invalid by resettinglast_returned, cache_blob, etc. That seems to be safe upon examination,but it's fragile and could easily get broken in future, which wouldn'tget revealed in testing with short-to-moderate-size strings.Per bug #17584 from James Inform. Whether or not the issue isreachable in the older branches, this code has been broken on itsown terms from its introduction, so patch all the way back.Discussion:https://postgr.es/m/17584-95c79b4a7d771f44@postgresql.org
1 parent4878ea7 commit9fe285f

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

‎src/backend/utils/adt/varlena.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ typedef struct
7878
char*buf1;/* 1st string, or abbreviation original string
7979
* buf */
8080
char*buf2;/* 2nd string, or abbreviation strxfrm() buf */
81-
intbuflen1;
82-
intbuflen2;
81+
intbuflen1;/* Allocated length of buf1 */
82+
intbuflen2;/* Allocated length of buf2 */
8383
intlast_len1;/* Length of last buf1 string/strxfrm() input */
8484
intlast_len2;/* Length of last buf2 string/strxfrm() blob */
8585
intlast_returned;/* Last comparison result (cache) */
@@ -2309,15 +2309,13 @@ varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
23092309

23102310
if (len1 >=sss->buflen1)
23112311
{
2312-
pfree(sss->buf1);
23132312
sss->buflen1=Max(len1+1,Min(sss->buflen1*2,MaxAllocSize));
2314-
sss->buf1=MemoryContextAlloc(ssup->ssup_cxt,sss->buflen1);
2313+
sss->buf1=repalloc(sss->buf1,sss->buflen1);
23152314
}
23162315
if (len2 >=sss->buflen2)
23172316
{
2318-
pfree(sss->buf2);
23192317
sss->buflen2=Max(len2+1,Min(sss->buflen2*2,MaxAllocSize));
2320-
sss->buf2=MemoryContextAlloc(ssup->ssup_cxt,sss->buflen2);
2318+
sss->buf2=repalloc(sss->buf2,sss->buflen2);
23212319
}
23222320

23232321
/*
@@ -2518,9 +2516,8 @@ varstr_abbrev_convert(Datum original, SortSupport ssup)
25182516
/* By convention, we use buffer 1 to store and NUL-terminate */
25192517
if (len >=sss->buflen1)
25202518
{
2521-
pfree(sss->buf1);
25222519
sss->buflen1=Max(len+1,Min(sss->buflen1*2,MaxAllocSize));
2523-
sss->buf1=palloc(sss->buflen1);
2520+
sss->buf1=repalloc(sss->buf1,sss->buflen1);
25242521
}
25252522

25262523
/* Might be able to reuse strxfrm() blob from last call */
@@ -2607,10 +2604,9 @@ varstr_abbrev_convert(Datum original, SortSupport ssup)
26072604
/*
26082605
* Grow buffer and retry.
26092606
*/
2610-
pfree(sss->buf2);
26112607
sss->buflen2=Max(bsize+1,
26122608
Min(sss->buflen2*2,MaxAllocSize));
2613-
sss->buf2=palloc(sss->buflen2);
2609+
sss->buf2=repalloc(sss->buf2,sss->buflen2);
26142610
}
26152611

26162612
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp