pnstrdup considered armed and dangerous - Mailing list pgsql-hackers

From Andres Freund
Subject pnstrdup considered armed and dangerous
Date
Msg-id 20161003215524.mwz5p45pcverrkyk@alap3.anarazel.de
Whole thread Raw
Responses Re: pnstrdup considered armed and dangerous  (Robert Haas <robertmhaas@gmail.com>)
Re: pnstrdup considered armed and dangerous  (Geoff Winkless <pgsqladmin@geoff.dj>)
Re: [HACKERS] pnstrdup considered armed and dangerous  (Andres Freund <andres@anarazel.de>)
List pgsql-hackers
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...

Greetings,

Andres Freund



pgsql-hackers by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: Question / requests.
Next
From: Michael Paquier
Date:
Subject: Re: pageinspect: Hash index support