Re: best place for xstrdup

Lists: pgsql-hackers
From: "Mendola Gaetano" <mendola(at)bigfoot(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Subject: best place for xstrdup
Date: 2003-09-16 14:49:03
Message-ID: 027e01c37c61$ac738670$152aa8c0@GMENDOLA2
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

As suggested by Bruce Mojiman I'm working on
substitute some strdup not checked with xstrdup.

I seen that in the backend source tree there is no
xstrdup ( there is one in bin/psql tree) ,
I wrote it and inserted temporarelly in
backend/utils/mmgr/aset.c

I don't know exactly how work the error report
mechanism sso before to submit a wrong implementation
this was what I wrote:

char *xstrdup(const char *string)
{
char * ret_value;

if ( !string ) {
elog(ERROR, "xstrdup called with a NULL pointer");
}

ret_value = strdup( string );

if ( !ret_value ) {
ereport(FATAL,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("strdup out of memory")));
}

return ret_value;
}

if this implementation is ok where is the best place ?

Regards
Gaetano Mendola


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Mendola Gaetano" <mendola(at)bigfoot(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: best place for xstrdup
Date: 2003-09-16 15:08:28
Message-ID: 17408.1063724908@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Mendola Gaetano" <mendola(at)bigfoot(dot)com> writes:
> if ( !ret_value ) {
> ereport(FATAL,
> (errcode(ERRCODE_OUT_OF_MEMORY),
> errmsg("strdup out of memory")));
> }

Should be ERROR not FATAL (the places that are insisting on FATAL are
special cases that won't be able to use this code). Also the text
should just be "out of memory".

Per previous discussion, I don't believe in the test for null input,
either ...

It might be worth creating an xmalloc as well, since I think there
are some unchecked malloc's in some places.

As for where to put it, mcxt.c may be the best place. aset.c does
not contain any routines that are supposed to be globally known.

regards, tom lane


From: "Mendola Gaetano" <mendola(at)bigfoot(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Cc: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: best place for xstrdup
Date: 2003-09-16 15:19:30
Message-ID: 028901c37c65$ed346180$152aa8c0@GMENDOLA2
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> "Mendola Gaetano" <mendola(at)bigfoot(dot)com> writes:
> > if ( !ret_value ) {
> > ereport(FATAL,
> > (errcode(ERRCODE_OUT_OF_MEMORY),
> > errmsg("strdup out of memory")));
> > }
>
> Should be ERROR not FATAL (the places that are insisting on FATAL are
> special cases that won't be able to use this code). Also the text
> should just be "out of memory".

Ok.

> Per previous discussion, I don't believe in the test for null input,
> either ...

Ok ok, I just tried :-)

> It might be worth creating an xmalloc as well, since I think there
> are some unchecked malloc's in some places.

I'll see

> As for where to put it, mcxt.c may be the best place. aset.c does
> not contain any routines that are supposed to be globally known.

Ok

Regards
Gaetano Mendola