Potential for bugs while using COPY_POINTER_FIELD to copy NULL pointer - Mailing list pgsql-hackers

From Ashutosh Bapat
Subject Potential for bugs while using COPY_POINTER_FIELD to copy NULL pointer
Date
Msg-id CAFjFpRcL3fPB2V3+k4=HqC87hbKFobJhXWztbEUqSKYB_fLwPw@mail.gmail.com
Whole thread Raw
Responses Re: Potential for bugs while using COPY_POINTER_FIELD to copy NULL pointer
List pgsql-hackers
Hi,<br />COPY_POINTER_FIELD is defined as - <br />  61 #define COPY_POINTER_FIELD(fldname, sz) \<br />  62     do {
\<br/>  63         Size    _size = (sz); \<br />  64         newnode->fldname = palloc(_size); \<br />  65        
memcpy(newnode->fldname,from->fldname, _size); \<br />   66     } while (0)<br /><br />Since we allocate _size
memoryirrespective of whether from->fldname is NULL, every NULL pointer can get copied as non-NULL pointer because
theway *alloc routines handle 0 sizes.<br />-- from man malloc<br /> If size  is  0,  then  malloc()  returns either
NULL,or a unique pointer value that can later be successfully passed to free()<br />--<br /><br />After such a copy
testslike if (pointer) will start failing. There are few callers of COPY_POINTER_FIELD which do not call the macro if
thesize can be 0. But there are some who do not do so. This looks fishy, in case we have if (pointer) kinds of
cases.<br/><br />Shouldn't COPY_POINTER_FIELD return NULL, if the pointer to be copied is NULL?<br />-- <br />Best
Wishes,<br/>Ashutosh Bapat<br />EntepriseDB Corporation<br />The Enterprise Postgres Company<br /><br /> 

pgsql-hackers by date:

Previous
From: Thom Brown
Date:
Subject: Re: pgsql_fdw, FDW for PostgreSQL server
Next
From: Clover White
Date:
Subject: why was the VAR 'optind' never changed in initdb?