Need to check palloc() return value? - Mailing list pgsql-general

From Michael Fuhr
Subject Need to check palloc() return value?
Date
Msg-id 20050216031355.GA14421@winnie.fuhr.org
Whole thread Raw
Responses Re: Need to check palloc() return value?
List pgsql-general
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/

pgsql-general by date:

Previous
From: "Andrew Hall"
Date:
Subject: Re: Lost rows/data corruption?
Next
From: "Greg Sabino Mullane"
Date:
Subject: Re: random record from small set