Re: BUG #17584: SQL crashes PostgreSQL when using ICU collation

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: James Inform <james(dot)inform(at)pharmapp(dot)de>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org, Euler Taveira <euler(at)eulerto(dot)com>, Peter Geoghegan <pg(at)bowt(dot)ie>
Subject: Re: BUG #17584: SQL crashes PostgreSQL when using ICU collation
Date: 2022-08-14 00:30:17
Message-ID: 541938.1660437017@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

James Inform <james(dot)inform(at)pharmapp(dot)de> writes:
> Here is the attachment.

Thanks for the test case! After tracing through it, I find that
the bug is not particularly related to ICU. It's in
varstr_abbrev_convert(): that function will sometimes reallocate
buffers in CurrentMemoryContext, which could be a shorter-lived
context than the one the SortSupport object belongs to. If so,
we'll eventually be scribbling on memory that doesn't belong
to us, and the observed problems are gripes from the scribble-ees.

I found this by valgrind'ing the test case, which eventually
printed this:

==00:00:00:27.370 497120== More than 10000000 total errors detected. I'm not reporting any more.
==00:00:00:27.370 497120== Final error counts will be inaccurate. Go fix your program!

and awhile later suffered an OOM kill. I got a good laugh
out of that --- never saw that valgrind message before.

Attached is a quick draft fix. Some notes:

* I'm not really proposing the added Asserts for commit,
though they helped provide confidence that I was on the
right track.

* We need to look around and see if the same mistake appears
in any other sortsupport code.

* The bug could never have existed at all if these buffer
resizings had been done with repalloc(). I get that the
idea is to avoid copying data we don't care about, but
this coding is still feeling like an antipattern. I wonder
if it'd be worth inventing a variant of repalloc that makes
the chunk bigger without preserving its contents.

regards, tom lane

Attachment Content-Type Size
fix-sort-support-misallocation-wip.patch text/x-diff 1.7 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Peter Geoghegan 2022-08-14 02:43:30 Re: BUG #17584: SQL crashes PostgreSQL when using ICU collation
Previous Message Tom Lane 2022-08-13 22:05:15 Re: BUG #17584: SQL crashes PostgreSQL when using ICU collation