Skip to content

Commit 88dc6fa

Browse files
committed
foreach() and list_delete() don't mix.
Fix crash when releasing duplicate entries in the encoding conversion cache list, caused by releasing the current entry of the list being chased by foreach(). We have a standard idiom for handling such cases, but this loop wasn't using it. This got broken in my recent rewrite of GUC assign hooks. Not sure how I missed this when testing the modified code, but I did. Per report from Peter.
1 parent d2f60a3 commit 88dc6fa

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/backend/utils/mb/mbutils.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ SetClientEncoding(int encoding)
189189
int current_server_encoding;
190190
bool found;
191191
ListCell *lc;
192+
ListCell *prev;
193+
ListCell *next;
192194

193195
if (!PG_VALID_FE_ENCODING(encoding))
194196
return -1;
@@ -222,10 +224,13 @@ SetClientEncoding(int encoding)
222224
* leak memory.
223225
*/
224226
found = false;
225-
foreach(lc, ConvProcList)
227+
prev = NULL;
228+
for (lc = list_head(ConvProcList); lc; lc = next)
226229
{
227230
ConvProcInfo *convinfo = (ConvProcInfo *) lfirst(lc);
228231

232+
next = lnext(lc);
233+
229234
if (convinfo->s_encoding == current_server_encoding &&
230235
convinfo->c_encoding == encoding)
231236
{
@@ -240,10 +245,13 @@ SetClientEncoding(int encoding)
240245
else
241246
{
242247
/* Duplicate entry, release it */
243-
ConvProcList = list_delete_ptr(ConvProcList, convinfo);
248+
ConvProcList = list_delete_cell(ConvProcList, lc, prev);
244249
pfree(convinfo);
250+
continue; /* prev mustn't advance */
245251
}
246252
}
253+
254+
prev = lc;
247255
}
248256

249257
if (found)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy