On 02.11.26 11:49, Peter Eisentraut wrote:
On 06.10.26 22:41, Tristan Partin wrote:
On Mon Oct 5, 2026 at 7:52 PM UTC, Peter Eisentraut wrote:
Ok, all the infrastructure is in place now. I'm working through the
attribute additions now.
One thing to think about is that adding these attributes to server
header files would also affect extensions. I think we might want to do
some research about that. Like, is the normal API to fill in the
structure yourself, or are there wrapper functions that are mandatory to
use? I'm going to park that for the moment and work through the
attribute additions in .c files first.
I have to think that extensions testing with
-fsanitize=bounds/_FORTIFY_SOURCE=3 is pretty rare. In any case, I did
some analysis (grepping and reading) on the list of structs exposed in
header files against 9 popular extensions (not in contrib), and I didn't
find any potential issues.
Ok, I studies this a bit, too, and I think it's ok. I have committed
the rest of this, and added a note in the commit message about this.
Since we committed these, clang with -fsanitize=bounds has been failing.
(CI only runs with gcc.)
The problem is that clang rejects things like
memcpy(&p->a[0], ...);
when the associated count is 0. It's debatable whether this is correct,
but in any case clang behaves inconsistently about it, and I have filed
a bug: https://github.com/llvm/llvm-project/issues/236753 . But there
hasn't been any response in a while, so I'm suggesting we go ahead with
a workaround. In this case, we can just write
memcpy(p->a, ...);
which is simpler anyway.
With the attached patch, I get a clean pass on clang -fsanitize=bounds.
From a22503f8c95655d4ce0917bfcc61d5d43deef325 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Thu, 26 Nov 2026 11:44:54 +0100
Subject: [PATCH] Fix clang bounds sanitizer failure with parallelCurrentXids
Commit 7a04cac0c3b annotated
SerializedTransactionState.parallelCurrentXids with the counted_by
attribute. Code that takes the address of the array as
¶llelCurrentXids[0] is then treated by clang's -fsanitize=bounds
as an access at index 0, which fails when nParallelCurrentXids is 0,
for example when a parallel query is run in a transaction without an
assigned XID. (The first change in this patch cannot fail because it
is guarded by a positive count, but it is changed for consistency.)
GCC allows this, and it's unclear whether this error by Clang is
intended or correct. See
<https://github.com/llvm/llvm-project/issues/236753> for a Clang bug
report.
To fix, use plain array-to-pointer decay instead. This is simpler and
more standard style anyway.
Discussion:
https://www.postgresql.org/message-id/flat/DKBEBF32QLUA.3L2DJRPUYNKHZ%40partin.io
---
src/backend/access/transam/xact.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/backend/access/transam/xact.c
b/src/backend/access/transam/xact.c
index d8d91bed427..623e8e3307c 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -5666,7 +5666,7 @@ SerializeTransactionState(Size maxsize, char
*start_address)
if (nParallelCurrentXids > 0)
{
result->nParallelCurrentXids = nParallelCurrentXids;
- memcpy(&result->parallelCurrentXids[0], ParallelCurrentXids,
+ memcpy(result->parallelCurrentXids, ParallelCurrentXids,
nParallelCurrentXids * sizeof(TransactionId));
return;
}
@@ -5702,7 +5702,7 @@ SerializeTransactionState(Size maxsize, char
*start_address)
/* Copy data into output area. */
result->nParallelCurrentXids = nxids;
- memcpy(&result->parallelCurrentXids[0], workspace,
+ memcpy(result->parallelCurrentXids, workspace,
nxids * sizeof(TransactionId));
}
@@ -5727,7 +5727,7 @@ StartParallelWorkerTransaction(char *tstatespace)
tstate->currentFullTransactionId;
currentCommandId = tstate->currentCommandId;
nParallelCurrentXids = tstate->nParallelCurrentXids;
- ParallelCurrentXids = &tstate->parallelCurrentXids[0];
+ ParallelCurrentXids = tstate->parallelCurrentXids;
CurrentTransactionState->blockState = TBLOCK_PARALLEL_INPROGRESS;
}
--
2.56.0