Fix possible dereference null pointer (PQprint)

Started by Ranier Vilelaover 2 years ago3 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

appliessuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t49695
psql -h localhost -U postgres

Built from patchset v1 (message #1), September 20, 2026 at 10:16 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t49695_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t49695_1 && git checkout t49695_1

Patchset v1 (message #1) is on t49695_1

Jump to latest
#1Ranier Vilela
ranier.vf@gmail.com

Hi.

In the function *PQprint*, the variable po->fieldName
can be NULL.
See the checks a few lines up.

for (numFieldName = 0;
po->fieldName && po->fieldName[numFieldName];
numFieldName++)

So, I think that must be checked, when used,
in the loop below.

best regards,
Ranier Vilela

Attachments:

t49695_1
fix-possible-dereference-null-pointer-PQprint.patchapplication/octet-stream; name=fix-possible-dereference-null-pointer-PQprint.patchDownload+1-1
#2Daniel Gustafsson
daniel@yesql.se
In reply to: Ranier Vilela (#1)
Re: Fix possible dereference null pointer (PQprint)

On 27 May 2024, at 16:52, Ranier Vilela <ranier.vf@gmail.com> wrote:

In the function *PQprint*, the variable po->fieldName can be NULL.

Yes.

See the checks a few lines up.

Indeed, let's check it.

for (numFieldName = 0;
po->fieldName && po->fieldName[numFieldName];
numFieldName++)
;
for (j = 0; j < nFields; j++)
{
int len;
const char *s = (j < numFieldName && po->fieldName[j][0]) ?
po->fieldName[j] : PQfname(res, j);

If po->fieldName is NULL then numFieldName won't be incremented and will remain
zero. In the check you reference we check (j < numFieldName) which will check
the j in the range 0..nFields for being less than zero. The code thus does
seem quite correct to me.

--
Daniel Gustafsson

#3Ranier Vilela
ranier.vf@gmail.com
In reply to: Daniel Gustafsson (#2)
Re: Fix possible dereference null pointer (PQprint)

Em sex., 31 de mai. de 2024 às 05:03, Daniel Gustafsson <daniel@yesql.se>
escreveu:

On 27 May 2024, at 16:52, Ranier Vilela <ranier.vf@gmail.com> wrote:

In the function *PQprint*, the variable po->fieldName can be NULL.

Yes.

See the checks a few lines up.

Indeed, let's check it.

for (numFieldName = 0;
po->fieldName && po->fieldName[numFieldName];
numFieldName++)
;
for (j = 0; j < nFields; j++)
{
int len;
const char *s = (j < numFieldName && po->fieldName[j][0]) ?
po->fieldName[j] : PQfname(res, j);

If po->fieldName is NULL then numFieldName won't be incremented and will
remain
zero. In the check you reference we check (j < numFieldName) which will
check
the j in the range 0..nFields for being less than zero. The code thus does
seem quite correct to me.

You are completely correct. My bad.

Thank you Daniel.

best regards,
Ranier Vilela