Re: [HACKERS] pnstrdup considered armed and dangerous

Поиск
Список
Период
Сортировка
От Andres Freund
Тема Re: [HACKERS] pnstrdup considered armed and dangerous
Дата
Msg-id 20171003065502.4tve5ek2z46xdb6h@alap3.anarazel.de
обсуждение исходный текст
Ответ на pnstrdup considered armed and dangerous  (Andres Freund <andres@anarazel.de>)
Список pgsql-hackers
On 2016-10-03 14:55:24 -0700, Andres Freund wrote:
> Hi,
> 
> A colleage of me just wrote innocent looking code like
>         char *shardRelationName = pnstrdup(relationName, NAMEDATALEN);
> which is at the moment wrong if relationName isn't preallocated to
> NAMEDATALEN size.
> 
> /*
>  * pnstrdup
>  *        Like pstrdup(), but append null byte to a
>  *        not-necessarily-null-terminated input string.
>  */
> char *
> pnstrdup(const char *in, Size len)
> {
>     char       *out = palloc(len + 1);
> 
>     memcpy(out, in, len);
>     out[len] = '\0';
>     return out;
> }
> 
> isn't that a somewhat weird behaviour / implementation? Not really like
> strndup(), which one might believe to be analoguous...

I've since hit this bug again. To fix it, you'd need strnlen. The lack
of which I'd also independently hit twice.  So here's a patch adding
pg_strnlen and using that to fix pnstrdup.

Greetings,

Andres Freund

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Вложения

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Kyotaro HORIGUCHI
Дата:
Сообщение: Re: [HACKERS] proposal - Default namespaces for XPath expressions(PostgreSQL 11)
Следующее
От: Andreas Seltenreich
Дата:
Сообщение: Re: [HACKERS] [sqlsmith] crash in RestoreLibraryState during low-memory testing