Re: BUG #18230: Redundant comparison of a local variable 'tzp' address with a NULL value at dt_common.c

Lists: pgsql-bugs
From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: rekgrpth(at)gmail(dot)com
Subject: BUG #18230: Redundant comparison of a local variable 'tzp' address with a NULL value at dt_common.c
Date: 2023-12-06 04:49:39
Message-ID: 18230-c68eb05e05cb9177@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 18230
Logged by: RekGRpth
Email address: rekgrpth(at)gmail(dot)com
PostgreSQL version: 16.1
Operating system: all
Description:

All versions of PostgreSQL has redundant comparison of a local variable
'tzp' address with a NULL value at dt_common.c in DecodeDateTime function.

```c
...
int t = 0;
int *tzp = &t;
...
if (tzp != NULL)
...

if (tzp == NULL)
...
```


From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: rekgrpth(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18230: Redundant comparison of a local variable 'tzp' address with a NULL value at dt_common.c
Date: 2023-12-06 13:55:25
Message-ID: da26cdf3558c605244838ca899c96c8874bbe92d.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Wed, 2023-12-06 at 04:49 +0000, PG Bug reporting form wrote:
> The following bug has been logged on the website:
>
> Bug reference:      18230
> Logged by:          RekGRpth
> Email address:      rekgrpth(at)gmail(dot)com
> PostgreSQL version: 16.1
> Operating system:   all
> Description:       
>
> All versions of PostgreSQL has redundant comparison of a local variable
> 'tzp' address with a NULL value at dt_common.c in DecodeDateTime function.
>
> ```c
> ...
> int t = 0;
> int    *tzp = &t;
> ...
> if (tzp != NULL)
> ...
>
> if (tzp == NULL)

That's not really a bug, but should certainly be improved.

At fault is commit 635a0b9a864, which removed "tzp" as a parameter from
"DecodeDateTime" and replaced it with a constant pointer to 0, when it
should have done more, like remove the variable and its uses.

Do you want to write a patch?

Yours,
Laurenz Albe


From: RekGRpth <rekgrpth(at)gmail(dot)com>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18230: Redundant comparison of a local variable 'tzp' address with a NULL value at dt_common.c
Date: 2023-12-06 14:44:54
Message-ID: CAPgh2mJJ7XrHTWSnYEb92R4PoknrqopOKDe6+795=v3q1aD6Eg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

something like?

ср, 6 дек. 2023 г. в 18:55, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>:
>
> On Wed, 2023-12-06 at 04:49 +0000, PG Bug reporting form wrote:
> > The following bug has been logged on the website:
> >
> > Bug reference: 18230
> > Logged by: RekGRpth
> > Email address: rekgrpth(at)gmail(dot)com
> > PostgreSQL version: 16.1
> > Operating system: all
> > Description:
> >
> > All versions of PostgreSQL has redundant comparison of a local variable
> > 'tzp' address with a NULL value at dt_common.c in DecodeDateTime function.
> >
> > ```c
> > ...
> > int t = 0;
> > int *tzp = &t;
> > ...
> > if (tzp != NULL)
> > ...
> >
> > if (tzp == NULL)
>
> That's not really a bug, but should certainly be improved.
>
> At fault is commit 635a0b9a864, which removed "tzp" as a parameter from
> "DecodeDateTime" and replaced it with a constant pointer to 0, when it
> should have done more, like remove the variable and its uses.
>
> Do you want to write a patch?
>
> Yours,
> Laurenz Albe

Attachment Content-Type Size
tzp.diff text/x-patch 4.1 KB

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: RekGRpth <rekgrpth(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18230: Redundant comparison of a local variable 'tzp' address with a NULL value at dt_common.c
Date: 2023-12-06 16:38:02
Message-ID: 51a453dfe23d1b7f33c2d9b495a4c6eb972da61e.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Wed, 2023-12-06 at 19:44 +0500, RekGRpth wrote:
> something like?

Yes, perhaps - although the variable name "tzp" = "time zone pointer"
is not appropriate any more.

Perhaps the "tzp" variable could be left as it is, and only the
comparisons "if (tzp == NULL)" be removed.

Yours,
Laurenz Albe


From: RekGRpth <rekgrpth(at)gmail(dot)com>
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18230: Redundant comparison of a local variable 'tzp' address with a NULL value at dt_common.c
Date: 2023-12-06 16:41:00
Message-ID: CAPgh2mKi8t8T7Tsqx-EwSMYG_xYQQSY30qp18sH8dVySP4Fmdg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Fixed!

ср, 6 дек. 2023 г. в 21:38, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>:
>
> On Wed, 2023-12-06 at 19:44 +0500, RekGRpth wrote:
> > something like?
>
> Yes, perhaps - although the variable name "tzp" = "time zone pointer"
> is not appropriate any more.
>
> Perhaps the "tzp" variable could be left as it is, and only the
> comparisons "if (tzp == NULL)" be removed.
>
> Yours,
> Laurenz Albe
>

Attachment Content-Type Size
tzp2.diff text/x-patch 2.5 KB

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: RekGRpth <rekgrpth(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18230: Redundant comparison of a local variable 'tzp' address with a NULL value at dt_common.c
Date: 2023-12-06 21:01:58
Message-ID: a0a114a7068a783a9ca4229d194059eeb85533f9.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Wed, 2023-12-06 at 21:41 +0500, RekGRpth wrote:
> Fixed!

That patch looks good to me.

Yours,
Laurenz Albe


From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: RekGRpth <rekgrpth(at)gmail(dot)com>
Cc: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18230: Redundant comparison of a local variable 'tzp' address with a NULL value at dt_common.c
Date: 2023-12-07 01:51:57
Message-ID: CAMbWs48z_7Vc8_Q93QEuUaB6n3-rJPO0f8KF=gbogzTmK4hvpA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Thu, Dec 7, 2023 at 12:41 AM RekGRpth <rekgrpth(at)gmail(dot)com> wrote:

> Fixed!

Nice catch.

There are several places in DecodeDateTime() where the value of '*tzp'
is changed. It seems to me that these assignments are unnecessary since
the value of '*tzp' is not utilized anywhere in the code. Can we also
remove these assignments?

Thanks
Richard


From: RekGRpth <rekgrpth(at)gmail(dot)com>
To: Richard Guo <guofenglinux(at)gmail(dot)com>
Cc: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18230: Redundant comparison of a local variable 'tzp' address with a NULL value at dt_common.c
Date: 2023-12-07 02:28:02
Message-ID: CAPgh2mKNpTfMJ7G3QF_cxLDsvQzMDGc2-XoL41oajt5VgO5ZNA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Fixed!

чт, 7 дек. 2023 г. в 06:52, Richard Guo <guofenglinux(at)gmail(dot)com>:
>
>
> On Thu, Dec 7, 2023 at 12:41 AM RekGRpth <rekgrpth(at)gmail(dot)com> wrote:
>>
>> Fixed!
>
>
> Nice catch.
>
> There are several places in DecodeDateTime() where the value of '*tzp'
> is changed. It seems to me that these assignments are unnecessary since
> the value of '*tzp' is not utilized anywhere in the code. Can we also
> remove these assignments?
>
> Thanks
> Richard

Attachment Content-Type Size
tz.diff text/x-patch 4.1 KB

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Richard Guo <guofenglinux(at)gmail(dot)com>
Cc: RekGRpth <rekgrpth(at)gmail(dot)com>, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18230: Redundant comparison of a local variable 'tzp' address with a NULL value at dt_common.c
Date: 2023-12-07 02:32:33
Message-ID: ZXEuwRR0Sq012eiu@paquier.xyz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Thu, Dec 07, 2023 at 09:51:57AM +0800, Richard Guo wrote:
> There are several places in DecodeDateTime() where the value of '*tzp'
> is changed. It seems to me that these assignments are unnecessary since
> the value of '*tzp' is not utilized anywhere in the code. Can we also
> remove these assignments?

*tzp could be given to DecodeTimezone() and manipulated inside it, but
you are right that we don't use it at all in the DecodeDateTime()
path. Other callers of DecodeTimezone() may depend on the changes
done inside it, though. Perhaps DecodeTimezone() should be extended
so as it can accept a NULL value and use that in DecodeDateTime(),
eliminating the need for *tzp entirely?
--
Michael


From: RekGRpth <rekgrpth(at)gmail(dot)com>
To: Michael Paquier <michael(at)paquier(dot)xyz>
Cc: Richard Guo <guofenglinux(at)gmail(dot)com>, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18230: Redundant comparison of a local variable 'tzp' address with a NULL value at dt_common.c
Date: 2023-12-07 02:39:35
Message-ID: CAPgh2mJYYbjqk-Qk8mRGTPO1bWnkGngQf1BDucNUYNN9hoDWmw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Fixed!

чт, 7 дек. 2023 г. в 07:32, Michael Paquier <michael(at)paquier(dot)xyz>:
>
> On Thu, Dec 07, 2023 at 09:51:57AM +0800, Richard Guo wrote:
> > There are several places in DecodeDateTime() where the value of '*tzp'
> > is changed. It seems to me that these assignments are unnecessary since
> > the value of '*tzp' is not utilized anywhere in the code. Can we also
> > remove these assignments?
>
> *tzp could be given to DecodeTimezone() and manipulated inside it, but
> you are right that we don't use it at all in the DecodeDateTime()
> path. Other callers of DecodeTimezone() may depend on the changes
> done inside it, though. Perhaps DecodeTimezone() should be extended
> so as it can accept a NULL value and use that in DecodeDateTime(),
> eliminating the need for *tzp entirely?
> --
> Michael

Attachment Content-Type Size
rm_tzp.diff text/x-patch 4.5 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Paquier <michael(at)paquier(dot)xyz>
Cc: Richard Guo <guofenglinux(at)gmail(dot)com>, RekGRpth <rekgrpth(at)gmail(dot)com>, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18230: Redundant comparison of a local variable 'tzp' address with a NULL value at dt_common.c
Date: 2023-12-07 04:05:04
Message-ID: 1196403.1701921904@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Michael Paquier <michael(at)paquier(dot)xyz> writes:
> *tzp could be given to DecodeTimezone() and manipulated inside it, but
> you are right that we don't use it at all in the DecodeDateTime()
> path. Other callers of DecodeTimezone() may depend on the changes
> done inside it, though. Perhaps DecodeTimezone() should be extended
> so as it can accept a NULL value and use that in DecodeDateTime(),
> eliminating the need for *tzp entirely?

TBH, I think our best path here is to leave well enough alone.
The main impact of the proposed changes is to make ecpg's copy
of the datetime parsing code diverge even further from the
backend's copy. I don't think that these changes buy anything
meaningful, and what they will do is make it harder to reunify
that logic, which somebody will probably try to do someday.

(The recent changes to support non-error-throwing returns from
input functions probably made that task notably easier, BTW.)

regards, tom lane