Re: A latent pointer increment bug in pgtypes_defmt_scan()

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: A latent pointer increment bug in pgtypes_defmt_scan()
Date: 2026-11-19 10:38:00
Message-ID: af69c017-ed46-4ccd-ad85-b6bbdfc08576@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 19/11/2026 09:22, Kyotaro Horiguchi wrote:
> Hello,
>
> While looking at another patch, I noticed the following code.
>
> dt_common.c:2485:
>> while (**pstr == ' ')
>> pstr++;
>
> Since *pstr points to a single string, and the code later uses
> "while (**pstr == ' ') (*pstr)++;", I think this was intended to skip
> leading spaces, but actually advances the wrong pointer.
>
> If this code were actually executed, it could cause a crash in the
> worst case. However, the caller already skips leading spaces in
> **pstr, so this is effectively dead code and probably hasn't caused
> any problems.
>
> I think the function itself needs to skip leading spaces to locate the
> start of a token, so this part should be fixed as follows.
>
> while (**pstr == ' ')
> - pstr++;
> + (*pstr)++;

Huh, yeah, that's clearly wrong.

This code really wants to skip leading spaces. It does it in:

- the loop in PGTYPEStimestamp_defmt_scan()
- this broken snippet in pgtypes_defmt_scan()
- one of the codepaths in find_end_token()
- two of the switch cases in pgtypes_defmt_scan()

With the skipping in PGTYPEStimestamp_defmt_scan(), all of those other
snippets are redundant.

This code could use some love, but I just committed your one-line fix
for now.

- Heikki

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2026-11-19 10:48:57 Re: Add a hook for handling logical decoding messages on subscribers.
Previous Message vignesh C 2026-11-19 10:33:06 Re: Initial table sync copies the whole table when it is no longer published