Unix Technical Forum

not checking value returned from palloc?

This is a discussion on not checking value returned from palloc? within the pgsql Hackers forums, part of the PostgreSQL category; --> Looking through the postgresql source code, I notice that there are many places were palloc is used but the ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Hackers

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-12-2008, 01:38 AM
Mark Dilger
 
Posts: n/a
Default not checking value returned from palloc?

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-12-2008, 01:38 AM
Tom Lane
 
Posts: n/a
Default Re: not checking value returned from palloc?

Mark Dilger <pgsql@markdilger.com> writes:
> 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.

Any place that *does* check it is incorrect (in the sense of being
wasted code space, not functionally incorrect). Please read the memmgr
documentation.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-12-2008, 01:38 AM
Peter Eisentraut
 
Posts: n/a
Default Re: not checking value returned from palloc?

Mark Dilger wrote:
> 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.


palloc will throw an exception if it cannot fulfill the request. Code
that checks the return value for null is in fact a waste.

--
Peter Eisentraut
http://developer.postgresql.org/~petere/

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-12-2008, 01:38 AM
Mark Dilger
 
Posts: n/a
Default Re: not checking value returned from palloc?

Peter Eisentraut wrote:
> Mark Dilger wrote:
>
>>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.

>
>
> palloc will throw an exception if it cannot fulfill the request. Code
> that checks the return value for null is in fact a waste.
>


Interesting. So the patch should go the other way, and remove the checks that
are currently in the code?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-12-2008, 01:38 AM
Alvaro Herrera
 
Posts: n/a
Default Re: not checking value returned from palloc?

Mark Dilger wrote:
> Peter Eisentraut wrote:
> >Mark Dilger wrote:
> >
> >>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.

> >
> >palloc will throw an exception if it cannot fulfill the request. Code
> >that checks the return value for null is in fact a waste.

>
> Interesting. So the patch should go the other way, and remove the checks
> that are currently in the code?


Yes, if you find any such place please submit a patch.

--
Alvaro Herrera http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-12-2008, 01:38 AM
Bruce Momjian
 
Posts: n/a
Default Re: not checking value returned from palloc?

Alvaro Herrera wrote:
> Mark Dilger wrote:
> > Peter Eisentraut wrote:
> > >Mark Dilger wrote:
> > >
> > >>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.
> > >
> > >palloc will throw an exception if it cannot fulfill the request. Code
> > >that checks the return value for null is in fact a waste.

> >
> > Interesting. So the patch should go the other way, and remove the checks
> > that are currently in the code?

>
> Yes, if you find any such place please submit a patch.


I see one in cash.c that I will remove.

--
Bruce Momjian http://candle.pha.pa.us
SRA OSS, Inc. http://www.sraoss.com

+ If your life is a hard drive, Christ can be your backup. +

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-12-2008, 01:39 AM
Neil Conway
 
Posts: n/a
Default Re: not checking value returned from palloc?

On Sun, 2006-03-19 at 17:14 -0500, Bruce Momjian wrote:
> I see one in cash.c that I will remove.


I've already checked in a fix for that, as well as a few other places
that made similar mistakes -- sorry for stepping on your toes.

-Neil



---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:08 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com