Thread: Need to check palloc() return value?

Need to check palloc() return value?

From
Michael Fuhr
Date:
Do user-defined functions need to check palloc()'s return value,
or does return guarantee success?  The latter appears to be the
case:

Datum
palloctest(PG_FUNCTION_ARGS)
{
    int32  nbytes = PG_GETARG_INT32(0);
    char  *p;
    ereport(INFO, (errmsg("calling palloc")));
    p = palloc(nbytes);
    ereport(INFO, (errmsg("palloc returned")));
    PG_RETURN_INT32(nbytes);
}

SELECT palloctest(1000);
INFO:  calling palloc
INFO:  palloc returned
 palloctest
------------
       1000
(1 row)

\set VERBOSITY verbose
SELECT palloctest(1000000000);
INFO:  00000: calling palloc
LOCATION:  palloctest, palloctest.c:34
ERROR:  53200: out of memory
DETAIL:  Failed on request of size 1000000000.
LOCATION:  AllocSetAlloc, aset.c:505

Control doesn't return to the user-defined function if the allocation
fails, so checking palloc()'s return value seems superfluous.  Is it
safe to rely on this behavior?

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

Re: Need to check palloc() return value?

From
Neil Conway
Date:
On Tue, 2005-02-15 at 20:13 -0700, Michael Fuhr wrote:
> Do user-defined functions need to check palloc()'s return value,
> or does return guarantee success?

It guarantees success.

-Neil