Re: B-Tree support function number 3 (strxfrm() optimization)

From: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>
To: Peter Geoghegan <pg(at)heroku(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>, Robert Haas <robertmhaas(at)gmail(dot)com>
Subject: Re: B-Tree support function number 3 (strxfrm() optimization)
Date: 2014-03-31 15:08:25
Message-ID: 533984E9.8040808@vmware.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 03/31/2014 08:51 AM, Peter Geoghegan wrote:
> + #ifdef HAVE_LOCALE_T
> + if (tss->locale)
> + strxfrm_l(pres, tss->buf1, Min(sizeof(Datum), len), tss->locale);
> + else
> + #endif
> + strxfrm(pres, tss->buf1, Min(sizeof(Datum), len));
> +
> + pres[Min(sizeof(Datum) - 1, len)] = '\0';

I'm afraid that trick isn't 100% reliable. The man page for strxrfm says:

> RETURN VALUE
> The strxfrm() function returns the number of bytes required to store
> the transformed string in dest excluding the terminating null byte
> ('\0'). If the value returned is n or more, the contents of dest are
> indeterminate.

Note the last sentence. To avoid the undefined behavior, you have to
pass a buffer that's large enough to hold the whole result, and then
truncate the result.

- Heikki

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2014-03-31 15:54:13 Re: PQputCopyData dont signal error
Previous Message Robert Haas 2014-03-31 14:44:08 Re: Comment in src/backend/commands/vacuumlazy.c