Re: SetVariable

Lists: pgsql-hackers
From: "Mendola Gaetano" <mendola(at)bigfoot(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Subject: SetVariable
Date: 2003-08-27 14:58:50
Message-ID: 000b01c36cab$b9fe8b20$152aa8c0@GMENDOLA2
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi all,
I found this code on the file variables.c and
in the function SetVariable I read:

if (strcmp(current->name, name) == 0)
{
free(current->value);
current->value = strdup(value);
return current->value ? true : false;
}

this mean that if there is no memory left on the
sistem we loose the old value,
if this is not the indeended behaviour may be is better do:

if (strcmp(current->name, name) == 0)
{
char * tmp_value = strdup(value);

if ( !tmp_value )
{
return false;
}

free(current->value);
current->value = tmp_value;

return true;
}

Regards
Gaetano Mendola


From: "Mendola Gaetano" <mendola(at)bigfoot(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: SetVariable
Date: 2003-08-28 17:58:09
Message-ID: 00e101c36d8d$f1580640$152aa8c0@GMENDOLA2
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Just a follow up,
is it better to give a patch for this kind of stuff ?

Regards
Gaetano Mendola

""Mendola Gaetano"" <mendola(at)bigfoot(dot)com> wrote:
> Hi all,
> I found this code on the file variables.c and
> in the function SetVariable I read:
>
> if (strcmp(current->name, name) == 0)
> {
> free(current->value);
> current->value = strdup(value);
> return current->value ? true : false;
> }
>
> this mean that if there is no memory left on the
> sistem we loose the old value,
> if this is not the indeended behaviour may be is better do:
>
> if (strcmp(current->name, name) == 0)
> {
> char * tmp_value = strdup(value);
>
> if ( !tmp_value )
> {
> return false;
> }
>
> free(current->value);
> current->value = tmp_value;
>
> return true;
> }
>
>
> Regards
> Gaetano Mendola
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend
>


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Mendola Gaetano <mendola(at)bigfoot(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: SetVariable
Date: 2003-08-30 04:12:24
Message-ID: 200308300412.h7U4COH22266@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Mendola Gaetano wrote:
> Hi all,
> I found this code on the file variables.c and
> in the function SetVariable I read:
>
> if (strcmp(current->name, name) == 0)
> {
> free(current->value);
> current->value = strdup(value);
> return current->value ? true : false;
> }
>
> this mean that if there is no memory left on the
> sistem we loose the old value,
> if this is not the indeended behaviour may be is better do:

I see other strdup() calls that don't check on a return. Should we deal
with those too?

>
> if (strcmp(current->name, name) == 0)
> {
> char * tmp_value = strdup(value);
>
> if ( !tmp_value )
> {
> return false;
> }
>
> free(current->value);
> current->value = tmp_value;
>
> return true;
> }
>
>
> Regards
> Gaetano Mendola
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend
>

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: "Gaetano Mendola" <mendola(at)bigfoot(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Cc: "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us>
Subject: Re: SetVariable
Date: 2003-08-30 06:29:01
Message-ID: 001201c36ec0$0091fed0$10d4a8c0@mm.eutelsat.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us> wrote:
> I see other strdup() calls that don't check on a return. Should we deal
> with those too?

Well strdup obtain the memory for the new string using a malloc
and normally is a good habit check the return value of a malloc.

Regards
Gaetano Mendola


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Gaetano Mendola <mendola(at)bigfoot(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: SetVariable
Date: 2003-08-30 13:58:56
Message-ID: 200308301358.h7UDwu427952@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Gaetano Mendola wrote:
> "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us> wrote:
> > I see other strdup() calls that don't check on a return. Should we deal
> > with those too?
>
> Well strdup obtain the memory for the new string using a malloc
> and normally is a good habit check the return value of a malloc.

Right. My point is that we have lots of other strdup's in the code.
Should we fix those too? Seems we should be consistent.

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: Gaetano Mendola <mendola(at)bigfoot(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: SetVariable
Date: 2003-08-30 15:03:40
Message-ID: 24805.1062255820@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> I see other strdup() calls that don't check on a return. Should we deal
> with those too?

strdup -> xstrdup if you're concerned.

regards, tom lane


From: "Gaetano Mendola" <mendola(at)bigfoot(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Cc: "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us>
Subject: Re: SetVariable
Date: 2003-08-30 17:58:03
Message-ID: 000d01c36f20$42e95390$10d4a8c0@mm.eutelsat.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us> wrote:
> Gaetano Mendola wrote:
> > "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us> wrote:
> > > I see other strdup() calls that don't check on a return. Should we
deal
> > > with those too?
> >
> > Well strdup obtain the memory for the new string using a malloc
> > and normally is a good habit check the return value of a malloc.
>
> Right. My point is that we have lots of other strdup's in the code.
> Should we fix those too? Seems we should be consistent.

Of course yes, consider also that inside SetVariable the check is
performed but too late, after that the old value was loose for ever.

Keep also the suggestion of Tom Late about the xstrdup.

Regards
Gaetano Mendola


From: "Gaetano Mendola" <mendola(at)bigfoot(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Cc: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: SetVariable
Date: 2003-08-30 18:07:49
Message-ID: 001201c36f21$9fd7f4c0$10d4a8c0@mm.eutelsat.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> writes:
> > I see other strdup() calls that don't check on a return. Should we deal
> > with those too?
>
> strdup -> xstrdup if you're concerned.

May be is a good idea avoid the future use:

#define strdup STRDUP_DEPRECATED_USE_INSTEAD_XSTRDUP

I don't like the other solution: redefine the strdup in function of xstrdup.

Regards
Gaetano Mendola


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Gaetano Mendola" <mendola(at)bigfoot(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: SetVariable
Date: 2003-08-30 21:30:30
Message-ID: 258.1062279030@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Gaetano Mendola" <mendola(at)bigfoot(dot)com> writes:
> "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> strdup -> xstrdup if you're concerned.

> May be is a good idea avoid the future use:
> #define strdup STRDUP_DEPRECATED_USE_INSTEAD_XSTRDUP

Not a good idea --- there are places that want to check for strdup
failure and do something different than exit(1).

regards, tom lane


From: "Gaetano Mendola" <mendola(at)bigfoot(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Cc: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: SetVariable
Date: 2003-08-30 23:27:49
Message-ID: 000801c36f4e$53eda000$10d4a8c0@mm.eutelsat.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> "Gaetano Mendola" <mendola(at)bigfoot(dot)com> writes:
> > "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> >> strdup -> xstrdup if you're concerned.
>
> > May be is a good idea avoid the future use:
> > #define strdup STRDUP_DEPRECATED_USE_INSTEAD_XSTRDUP
>
> Not a good idea --- there are places that want to check for strdup
> failure and do something different than exit(1).

That's true.

Regards
Gaetano Mendola


From: "Mendola Gaetano" <mendola(at)bigfoot(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Cc: "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us>
Subject: Re: SetVariable
Date: 2003-09-01 16:55:19
Message-ID: 003901c370a9$d36bff00$32add6c2@mm.eutelsat.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us> wrote:
> Gaetano Mendola wrote:
> > "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us> wrote:
> > > I see other strdup() calls that don't check on a return. Should we
deal
> > > with those too?
> >
> > Well strdup obtain the memory for the new string using a malloc
> > and normally is a good habit check the return value of a malloc.
>
> Right. My point is that we have lots of other strdup's in the code.
> Should we fix those too? Seems we should be consistent.

Shall I post the patch for SetVariable ?

I know that this change is not so important but I want to try
on my skin the cycle
seen somethink wrong => send patch => be applyed

Regards
Gaetano Mendola


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Mendola Gaetano <mendola(at)bigfoot(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: SetVariable
Date: 2003-09-01 17:02:06
Message-ID: 200309011702.h81H26o17673@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Mendola Gaetano wrote:
> "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us> wrote:
> > Gaetano Mendola wrote:
> > > "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us> wrote:
> > > > I see other strdup() calls that don't check on a return. Should we
> deal
> > > > with those too?
> > >
> > > Well strdup obtain the memory for the new string using a malloc
> > > and normally is a good habit check the return value of a malloc.
> >
> > Right. My point is that we have lots of other strdup's in the code.
> > Should we fix those too? Seems we should be consistent.
>
> Shall I post the patch for SetVariable ?
>
> I know that this change is not so important but I want to try
> on my skin the cycle
> seen somethink wrong => send patch => be applyed

Sure.

It would be good if you would evaluate all the strdups, find the ones
that don't check properly, and submit a big patch of all those. The fix
can be to call xstrdup, or to check the return code.

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: "Gaetano Mendola" <mendola(at)bigfoot(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Cc: "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us>
Subject: Re: SetVariable
Date: 2003-09-01 22:34:48
Message-ID: 000a01c370d9$43f500d0$10d4a8c0@mm.eutelsat.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us> wrote:
> Mendola Gaetano wrote:
> > "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us> wrote:
> > > Gaetano Mendola wrote:
> > > > "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us> wrote:
> > > > > I see other strdup() calls that don't check on a return. Should
we
> > deal
> > > > > with those too?
> > > >
> > > > Well strdup obtain the memory for the new string using a malloc
> > > > and normally is a good habit check the return value of a malloc.
> > >
> > > Right. My point is that we have lots of other strdup's in the code.
> > > Should we fix those too? Seems we should be consistent.
> >
> > Shall I post the patch for SetVariable ?
> >
> > I know that this change is not so important but I want to try
> > on my skin the cycle
> > seen somethink wrong => send patch => be applyed
>
> Sure.
>
> It would be good if you would evaluate all the strdups, find the ones
> that don't check properly, and submit a big patch of all those. The fix
> can be to call xstrdup, or to check the return code.

I will.

Regards
Gaetano Mendola