not checking value returned from palloc?

From: Mark Dilger <pgsql(at)markdilger(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: not checking value returned from palloc?
Date: 2006-03-19 21:20:40
Message-ID: dvkhv5$sf7$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Looking through the postgresql source code, I notice that there are many places
were palloc is used but the return value is not checked to see if it is null.
There are a few places such as:

if (!PointerIsValid(result = palloc(CASH_BUFSZ + 2 - count +
strlen(nsymbol))))
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));

(taken from src/backend/utils/adt/cash.c), but at least within the that same
directory most occurrences of palloc are not checked.

Is this sloppy programming, or is there an automagical thing going on with
#defines that I'm just not seeing?

If it is just sloppy, perhaps we could use a new define in palloc.h, such as:

#define palloc_or_die(ptr,sz) \
do { \
ptr = palloc(sz); \
if (!ptr) \
{ \
ereport(ERROR, \
(errcode(ERRCODE_OUT_OF_MEMORY), \
errmsg("Out of memory"))); \
} \
} while(0);

And then, in all places where the code does not currently check the return value
of palloc, the code could be changed to use palloc_or_die instead. Of course,
I'd be happy if someone has a better name for the macro, perhaps something more
brief?

I can go over the code "with a fine tooth comb" and replace the offending
occurrences. Does the community think this is a good idea?

mark

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2006-03-19 21:38:49 Re: not checking value returned from palloc?
Previous Message Josh Berkus 2006-03-19 19:34:25 Re: [HACKERS] PostgreSQL Anniversary Proposals -- Important Update