[PATCH] contrib/xml2: guard against signed integer overflow in parse_params

Started by Varik Matevosyan5 months ago2 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.

won't retrysuccessCI 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:t139548
psql -h localhost -U postgres

Built from patchset v1 (message #1), August 17, 2026 at 12:42 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 t139548_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 t139548_1 && git checkout t139548_1

Patchset v1 (message #1) is on t139548_1

Jump to latest
#1Varik Matevosyan
varikmatevosyan@gmail.com

Hi,

Small robustness fix for contrib/xml2/parse_params. The doubling
of max_params relies on signed-integer overflow wrapping to a value
that AllocSizeIsValid then rejects, which is both UB and incidental
safety.

The overflow is unreachable in current builds (text input is bounded
by MaxAllocSize, which limits nparams below the doubling threshold),
but the fix is small and matches the explicit overflow-checking
idiom used elsewhere in the tree.

Patch attached against current master.

Regards,
Varik

Attachments:

t139548_1
0001-contrib-xml2-guard-against-signed-integer-overflow-i.patchapplication/octet-stream; name=0001-contrib-xml2-guard-against-signed-integer-overflow-i.patchDownload+8-2
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Varik Matevosyan (#1)
Re: [PATCH] contrib/xml2: guard against signed integer overflow in parse_params

Varik Matevosyan <varikmatevosyan@gmail.com> writes:

Small robustness fix for contrib/xml2/parse_params. The doubling
of max_params relies on signed-integer overflow wrapping to a value
that AllocSizeIsValid then rejects, which is both UB and incidental
safety.

There are many many places in our tree that handle that the same way.
The argument that it's UB is nonsense, because AllocSizeIsValid
rejects values >= 1G, so that it will fail on the iteration before
the integer counter can overflow. (This is indeed exactly why that
limit is 1G and not 2G; see the comment for MaxAllocSize.)

I think this proposal makes parse_params less like other code,
not more so, so I don't think we need extra code here.

regards, tom lane