]> granicus.if.org Git - postgresql/commit
Improve dubious memory management in pg_newlocale_from_collation().
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 20 Sep 2017 17:52:36 +0000 (13:52 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 20 Sep 2017 17:52:36 +0000 (13:52 -0400)
commit7b86c2ac9563ffd9b870cfd73a769431b7922e81
treef2eb53b3d1da675298eae5a208f8ca1e6ccc83e3
parent4939488af9b86edfff9b981773cd388d361c5830
Improve dubious memory management in pg_newlocale_from_collation().

pg_newlocale_from_collation() used malloc() and strdup() directly,
which is generally not per backend coding style, and it didn't bother
to check for failure results, but would just SIGSEGV instead.  Also,
if one of the numerous error checks in the middle of the function
failed, the already-allocated memory would be leaked permanently.
Admittedly, it's not a lot of memory, but it could build up if this
function were called repeatedly for a bad collation.

The first two problems are easily cured by palloc'ing in TopMemoryContext
instead of calling libc directly.  We can fairly easily dodge the leakage
problem for the struct pg_locale_struct by filling in a temporary variable
and allocating permanent storage only once we reach the bottom of the
function.  It's harder to get rid of the potential leakage for ICU's copy
of the collcollate string, but at least that's only allocated after most
of the error checks; so live with that aspect.

Back-patch to v10 where this code came in, with one or another of the
ICU patches.
src/backend/utils/adt/pg_locale.c