Avoid use scoped block variable

Started by Ranier Vilela10 months ago4 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:t52851
psql -h localhost -U postgres

Built from patchset v1 (message #1), September 20, 2026 at 09:22 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 t52851_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 t52851_1 && git checkout t52851_1

Patchset v1 (message #1) is on t52851_1

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

Hi.

I noticed a possible violation of C rules.
Some functions rely on local block variables,
but this are a mistake.
Once that block exits, the memory of the variable is released.

Fix by moving the declaration variables.

best regards,
Ranier Vilela

Attachments:

t52851_1
avoid-use-scoped-block-variable-lockfuncs.patchapplication/octet-stream; name=avoid-use-scoped-block-variable-lockfuncs.patchDownload+1-4
avoid-use-scoped-block-variable-rawpage.patchapplication/octet-stream; name=avoid-use-scoped-block-variable-rawpage.patchDownload+1-2
avoid-use-scoped-block-variable-walsender.patchapplication/octet-stream; name=avoid-use-scoped-block-variable-walsender.patchDownload+1-2
#2Andres Freund
andres@anarazel.de
In reply to: Ranier Vilela (#1)
Re: Avoid use scoped block variable

Hi,

On 2025-12-09 13:06:35 -0300, Ranier Vilela wrote:

I noticed a possible violation of C rules.
Some functions rely on local block variables,
but this are a mistake.
Once that block exits, the memory of the variable is released.

CStringGetTextDatum() copies its input to a fresh allocation. So there's no
longer-lived references to the local memory, unless I miss something?

Greetings,

Andres Freund

#3Tomas Vondra
tomas.vondra@2ndquadrant.com
In reply to: Ranier Vilela (#1)
Re: Avoid use scoped block variable

On 12/9/25 17:06, Ranier Vilela wrote:

Hi.

I noticed a possible violation of C rules.
Some functions rely on local block variables, 
but this are a mistake.
Once that block exits, the memory of the variable is released.

Fix by moving the declaration variables.

When you say "possible violation", did you check the issue is real?

All these places call CStringGetTextDatum, which calls cstring_to_text,
which allocates a new varlena copy of the string. So why is this an
issue, exactly?

regards

--
Tomas Vondra

#4Ranier Vilela
ranier.vf@gmail.com
In reply to: Andres Freund (#2)
Re: Avoid use scoped block variable

Em ter., 9 de dez. de 2025 às 13:19, Andres Freund <andres@anarazel.de>
escreveu:

Hi,

On 2025-12-09 13:06:35 -0300, Ranier Vilela wrote:

I noticed a possible violation of C rules.
Some functions rely on local block variables,
but this are a mistake.
Once that block exits, the memory of the variable is released.

CStringGetTextDatum() copies its input to a fresh allocation. So there's no
longer-lived references to the local memory, unless I miss something?

Yeah. My bad.
cstring_to_text use palloc.

Sorry for the noise.

best regards,
Ranier Vilela