pgsql: localbuf: Fix dangerous coding pattern in GetLocalVictimBuffer() - Mailing list pgsql-committers

From Andres Freund
Subject pgsql: localbuf: Fix dangerous coding pattern in GetLocalVictimBuffer()
Date
Msg-id E1ttda0-002gVz-0q@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
localbuf: Fix dangerous coding pattern in GetLocalVictimBuffer()

If PinLocalBuffer() were to modify the buf_state, the buf_state in
GetLocalVictimBuffer() would be out of date. Currently that does not happen,
as PinLocalBuffer() only modifies the buf_state if adjust_usagecount=true and
GetLocalVictimBuffer() passes false.

However, it's easy to make this not the case anymore - it cost me a few hours
to debug the consequences.

The minimal fix would be to just refetch the buf_state after after calling
PinLocalBuffer(), but the same danger exists in later parts of the
function. Instead, declare buf_state in the narrower scopes and re-read the
state in conditional branches.  Besides being safer, it also fits well with
an upcoming set of cleanup patches that move the contents of the conditional
branches in GetLocalVictimBuffer() into helper functions.

I "broke" this in 794f2594479.

Arguably this should be backpatched, but as the relevant functions are not
exported and there is no actual misbehaviour, I chose to not backpatch, at
least for now.

Reviewed-by: Melanie Plageman <melanieplageman@gmail.com>
Discussion: https://postgr.es/m/CAAKRu_b9anbWzEs5AAF9WCvcEVmgz-1AkHSQ-CLLy-p7WHzvFw@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/fa6af9b25e4b449e6e006d9b3434315c0e8e4402

Modified Files
--------------
src/backend/storage/buffer/localbuf.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)


pgsql-committers by date:

Previous
From: Andrew Dunstan
Date:
Subject: pgsql: Silence perl critic
Next
From: Michael Paquier
Date:
Subject: pgsql: pg_createsubscriber: Remove some code bloat in the atexit() call