A latent pointer increment bug in pgtypes_defmt_scan()

Started by Kyotaro Horiguchi5 days ago4 messageshackers
Jump to latest
#1Kyotaro Horiguchi
horikyota.ntt@gmail.com

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)++;

Regards,

--
Kyotaro Horiguchi
NTT Open Source Software Center

Attachments:

t259445_1
v1-0001-Fix-incorrect-pointer-increment-in-pgtypes_defmt_.patchtext/x-patch; charset=us-asciiDownload+1-2
#2Chao Li
li.evan.chao@gmail.com
In reply to: Kyotaro Horiguchi (#1)
Re: A latent pointer increment bug in pgtypes_defmt_scan()

On Nov 19, 2026, at 15:22, Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote:

Hello,

While looking at another patch, I noticed the following code.

dt_common.c:2485:

while (**pstr == ' ')
pstr++;

Good catch. This seems like a typical pointer mistake made in a C class.

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 so. The only caller PGTYPEStimestamp_defmt_scan() has already skipped all leading spaces.

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)++;

Regards,

--
Kyotaro Horiguchi
NTT Open Source Software Center
<v1-0001-Fix-incorrect-pointer-increment-in-pgtypes_defmt_.patch>

The fix looks correct to me.

But maybe we can just remove the loop and add an assertion to ensure that str has no leading spaces?

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#3Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Kyotaro Horiguchi (#1)
Re: A latent pointer increment bug in pgtypes_defmt_scan()

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

#4Kyotaro Horiguchi
horikyota.ntt@gmail.com
In reply to: Heikki Linnakangas (#3)
Re: A latent pointer increment bug in pgtypes_defmt_scan()

Hello,

At Thu, 19 Nov 2026 12:38:00 +0200, Heikki Linnakangas <hlinnaka@iki.fi> wrote in

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.

Thanks for committing the fix.

I agree that those snippets are redundant, but I preferred to keep the
function self-contained. Further cleanup would involve the caller as
well, and I don't think it's worth the effort for now.

Regards,

--
Kyotaro Horiguchi
NTT Open Source Software Center