Re: when to use pfree? - Mailing list pgsql-general

From Tom Lane
Subject Re: when to use pfree?
Date
Msg-id 9813.1162911207@sss.pgh.pa.us
Whole thread Raw
In response to Re: when to use pfree?  (Ron Peterson <ron.peterson@yellowbank.com>)
List pgsql-general
Ron Peterson <ron.peterson@yellowbank.com> writes:
> char*
> tp2cp_palloc( char* stringp, const text* textp ) {
>    int len;
>    len = VARSIZE(textp) - VARHDRSZ;
>    stringp = (char*)palloc( len + 1 );
>    if( ! memcpy( stringp, VARDATA(textp), len ) ) { return NULL; }
>    if( ! memset( stringp + len, '\0', 1 ) ) { return NULL; }
>    return stringp;
> }

That's simply bizarre coding style.  stringp should be a local in
tp2cp_palloc, not a passed parameter that you ignore the value of.

> Which I call like

> otherfunc() {
>   char* rd;
>   if( ! tp2cp_palloc( rd, rand_dev ) )
>   ...

The above does not cause rd to become set in otherfunc().

Had you been using a reasonable set of compiler flags, the compiler
would have warned you that rd was uninitialized in otherfunc().

            regards, tom lane

pgsql-general by date:

Previous
From: Ron Peterson
Date:
Subject: Re: when to use pfree?
Next
From: TJ O'Donnell
Date:
Subject: Re: R and postgres