possible repalloc() in icu_convert_case()

Started by Anton Voloshinalmost 5 years ago3 messages
#1Anton Voloshin
a.voloshin@postgrespro.ru
1 attachment(s)

Hello,

in src/backend/utils/adt/formatting.c, in icu_convert_case() I see:
if (status == U_BUFFER_OVERFLOW_ERROR)
{
/* try again with adjusted length */
pfree(*buff_dest);
*buff_dest = palloc(len_dest * sizeof(**buff_dest));
...

Is there any reason why this should not be repalloc()?

In case it should be, I've attached a corresponding patch.

--
Anton Voloshin
Postgres Professional: https://www.postgrespro.com
Russian Postgres Company

Attachments:

repalloc-in-adt-formatting.patchtext/plain; charset=UTF-8; name=repalloc-in-adt-formatting.patchDownload
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c
index 783c7b5e7a..409067e4a0 100644
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -1588,8 +1588,7 @@ icu_convert_case(ICU_Convert_Func func, pg_locale_t mylocale,
    if (status == U_BUFFER_OVERFLOW_ERROR)
    {
        /* try again with adjusted length */
-       pfree(*buff_dest);
-       *buff_dest = palloc(len_dest * sizeof(**buff_dest));
+       *buff_dest = repalloc(*buff_dest, len_dest * sizeof(**buff_dest));
        status = U_ZERO_ERROR;
        len_dest = func(*buff_dest, len_dest, buff_source, len_source,
                        mylocale->info.icu.locale, &status);
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Anton Voloshin (#1)
Re: possible repalloc() in icu_convert_case()

Anton Voloshin <a.voloshin@postgrespro.ru> writes:

in src/backend/utils/adt/formatting.c, in icu_convert_case() I see:
if (status == U_BUFFER_OVERFLOW_ERROR)
{
/* try again with adjusted length */
pfree(*buff_dest);
*buff_dest = palloc(len_dest * sizeof(**buff_dest));
...

Is there any reason why this should not be repalloc()?

repalloc is likely to be more expensive, since it implies copying
data which isn't helpful here. I think this code is fine as-is.

regards, tom lane

#3Anton Voloshin
a.voloshin@postgrespro.ru
In reply to: Tom Lane (#2)
Re: possible repalloc() in icu_convert_case()

On 04.04.2021 19:20, Tom Lane wrote:

repalloc is likely to be more expensive, since it implies copying
data which isn't helpful here. I think this code is fine as-is.

Oh, you are right, thanks. I did not think properly about copying in
repalloc.

--
Anton Voloshin
Postgres Professional: https://www.postgrespro.com
Russian Postgres Company