Thread: pstrndup()

pstrndup()

From
Karel Zak
Date:
 Hi guys,

 we have pstrdup(char *string) and this tiny patch adds
 pstrndup(char *sting, Size len).

 By the  way, I  a little  played with the  apache memory  managment and
 they  have the  others interesting  routines like  ap_pstrcat(...) that
 concatenate  all  arguments  (last  must  be  NULL)  to  new  allocated
 string. Is something like this interesting for PostgreSQL?

    Karel

--
 Karel Zak  <zakkr@zf.jcu.cz>
 http://home.zf.jcu.cz/~zakkr/

Attachment

Re: pstrndup()

From
Bruce Momjian
Date:
Karel, do you plan to use pstrndup for some purpose?  I assume so.

---------------------------------------------------------------------------

Karel Zak wrote:
>
>  Hi guys,
>
>  we have pstrdup(char *string) and this tiny patch adds
>  pstrndup(char *sting, Size len).
>
>  By the  way, I  a little  played with the  apache memory  managment and
>  they  have the  others interesting  routines like  ap_pstrcat(...) that
>  concatenate  all  arguments  (last  must  be  NULL)  to  new  allocated
>  string. Is something like this interesting for PostgreSQL?
>
>     Karel
>
> --
>  Karel Zak  <zakkr@zf.jcu.cz>
>  http://home.zf.jcu.cz/~zakkr/

[ Attachment, skipping... ]

>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: pstrndup()

From
Tom Lane
Date:
Bruce Momjian <pgman@candle.pha.pa.us> writes:
> Karel, do you plan to use pstrndup for some purpose?  I assume so.

I am not familiar with strndup.  If the spec is like strncpy, I would
vote against including it ... strncpy is so broken that we had to invent
our own variant ...

            regards, tom lane

Re: pstrndup()

From
Karel Zak
Date:
On Sun, Mar 21, 2004 at 11:45:18PM -0500, Tom Lane wrote:
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > Karel, do you plan to use pstrndup for some purpose?  I assume so.

 I   think    PostgreSQL   should   supports   basic    operation   with
 allocation/strings if  it's open for  users' C functions and  we expect
 our own memory system usage.

> I am not familiar with strndup.  If the spec is like strncpy, I would
> vote against including it ... strncpy is so broken that we had to invent
> our own variant ...

 POSIX strncpy()  is different,  a result from  strncpy needn't  be zero
 terminated. You're right it's horrible function.

 The result of  strndup() is always zero terminated. It's  more safe and
 strndup() is  binary safe because  it doesn't check something  in input
 string. The pstrndup() is based on PostgreSQL memory managment.

    Karel

--
 Karel Zak  <zakkr@zf.jcu.cz>
 http://home.zf.jcu.cz/~zakkr/

Re: pstrndup()

From
Bruce Momjian
Date:
Karel Zak wrote:
> On Sun, Mar 21, 2004 at 11:45:18PM -0500, Tom Lane wrote:
> > Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > > Karel, do you plan to use pstrndup for some purpose?  I assume so.
>
>  I   think    PostgreSQL   should   supports   basic    operation   with
>  allocation/strings if  it's open for  users' C functions and  we expect
>  our own memory system usage.
>
> > I am not familiar with strndup.  If the spec is like strncpy, I would
> > vote against including it ... strncpy is so broken that we had to invent
> > our own variant ...
>
>  POSIX strncpy()  is different,  a result from  strncpy needn't  be zero
>  terminated. You're right it's horrible function.
>
>  The result of  strndup() is always zero terminated. It's  more safe and
>  strndup() is  binary safe because  it doesn't check something  in input
>  string. The pstrndup() is based on PostgreSQL memory managment.

Can you find places to use this function our backend?  Seems that should
be part of the patch.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: pstrndup()

From
Tom Lane
Date:
Bruce Momjian <pgman@candle.pha.pa.us> writes:
> Karel Zak wrote:
>> The result of  strndup() is always zero terminated. It's  more safe and
>> strndup() is  binary safe because  it doesn't check something  in input
>> string. The pstrndup() is based on PostgreSQL memory managment.

> Can you find places to use this function our backend?  Seems that should
> be part of the patch.

A bit of googling showed that strndup does appear in the "Linux
standards base", but it is not to be found in the Single Unix Spec.
That makes it at most quasi-standard IMHO.

Like Bruce, I'd like to see a more convincing use-case before adopting
not-very-standard functions.  I don't think people will expect pstrndup
to exist, or be very likely to use it if it's there, or necessarily know
what its behavior is (for sure I was confused by the naming similarity
with strncpy).  That's a lot of strikes for something that replaces only
three lines of C ...

            regards, tom lane

Re: pstrndup()

From
Karel Zak
Date:
On Mon, Mar 22, 2004 at 10:04:01AM -0500, Tom Lane wrote:
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > Karel Zak wrote:
> >> The result of  strndup() is always zero terminated. It's  more safe and
> >> strndup() is  binary safe because  it doesn't check something  in input
> >> string. The pstrndup() is based on PostgreSQL memory managment.
>
> > Can you find places to use this function our backend?  Seems that should
> > be part of the patch.
>
> A bit of googling showed that strndup does appear in the "Linux
> standards base", but it is not to be found in the Single Unix Spec.
> That makes it at most quasi-standard IMHO.

 man strndup

CONFORMING TO
     SVID 3, BSD 4.3

> with strncpy).  That's a lot of strikes for something that replaces only
> three lines of C ...

 How often are bugs with "add '\0' to last item of array"?

 Well, forget this patch...

    Karel

--
 Karel Zak  <zakkr@zf.jcu.cz>
 http://home.zf.jcu.cz/~zakkr/

Re: pstrndup()

From
Bruce Momjian
Date:
Karel Zak wrote:
> On Mon, Mar 22, 2004 at 10:04:01AM -0500, Tom Lane wrote:
> > Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > > Karel Zak wrote:
> > >> The result of  strndup() is always zero terminated. It's  more safe and
> > >> strndup() is  binary safe because  it doesn't check something  in input
> > >> string. The pstrndup() is based on PostgreSQL memory managment.
> >
> > > Can you find places to use this function our backend?  Seems that should
> > > be part of the patch.
> >
> > A bit of googling showed that strndup does appear in the "Linux
> > standards base", but it is not to be found in the Single Unix Spec.
> > That makes it at most quasi-standard IMHO.
>
>  man strndup
>
> CONFORMING TO
>      SVID 3, BSD 4.3


Strange, I don't see it on FreeBSD or BSD/OS.  I wonder if it was
removed in BSD 4.4.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073