On Mon Oct 5, 2026 at 2:37 PM UTC, Greg Burd wrote:
>
>> On Oct 5, 2026, at 7:51 AM, Peter Eisentraut <[email protected]> wrote:
>> 
>> On 29.09.26 07:50, Peter Eisentraut wrote:
>>> On 29.09.26 06:04, Tristan Partin wrote:
>>>> When compiling Postgres with -fsanitize=bounds, I get the following
>>>> errors in the log:
>>>> 
>>>>     jsonpath_exec.c:3832:3: runtime error: index 3 out of bounds for
>>>> type 'JsonbValue[2]'
>>>>     jsonpath_exec.c:3918:10: runtime error: index 3 out of bounds for
>>>> type 'JsonbValue[2]'
>>> 
>>> I've run into this problem as well, but only when compiling with -
>>> fstrict-flex-arrays=1.  Is that what you are using?
>>> 
>>> I would like to get this fixed, because this site appears to be the only
>>> problem of this kind.  I will check your proposal.
>> 
>> I played around with this a bit more and came up with the attached
>> solution, which is much simpler but requires that we don't go higher
>> than -fstrict-flex-arrays=1 -- which we can't anyway for other reasons
>> shown in the patch.
>> <v1.5-0001-Add-compiler-option-fstrict-flex-arrays-1.patch>
>
> Thanks Peter.
>
> I took a look.  It seems to make the right trade-offs for where we are
> and add value where we can.  I like the use of the union, too bad that
> struct isn't at the end so we could use FLEXIBLE_ARRAY_MEMBER.

I concur with Greg that it looks fine. Attached is another potential 
solution if we didn't want treewide -fstrict-flex-arrays=1. I saw that 
GCC supports a strict_flex_array attribute, which Clang does not 
currently support. Your solution is probably better for that reason, but 
I figured that I would post this anyway.

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)
diff --git i/src/backend/utils/adt/jsonpath_exec.c w/src/backend/utils/adt/jsonpath_exec.c
index 2191d67bfc0..ee24f0aa9b9 100644
--- i/src/backend/utils/adt/jsonpath_exec.c
+++ w/src/backend/utils/adt/jsonpath_exec.c
@@ -174,7 +174,21 @@ typedef struct JsonValueList
 	int			maxitems;		/* allocated length of items[] */
 	struct JsonValueList *next; /* => next chunk, if any */
 	struct JsonValueList *last; /* => last chunk (only valid in base chunk) */
-	JsonbValue	items[BASE_JVL_ITEMS];
+	union
+	{
+		/*
+		 * To satisfy -fstrict-flex-arrays=1, we need one union member that
+		 * makes space for the base chunk and one member that is effectively
+		 * declared as a flexible-array member, otherwise you'd get
+		 * -fsanitize=undefined errors.  Note that we can't declare items as
+		 * [FLEXIBLE_ARRAY_MEMBER], because JsonValueList is used in a
+		 * non-last field of JsonTablePlanState, and that's not allowed with
+		 * flexible-array members.  But using [1] works as long as we don't go
+		 * higher than -fstrict-flex-arrays=1.
+		 */
+		JsonbValue	base_items[BASE_JVL_ITEMS];
+		JsonbValue	items[1] pg_attribute_strict_flex_array(1);
+	};
 } JsonValueList;
 
 /* State data for iterating through a JsonValueList */
diff --git i/src/include/c.h w/src/include/c.h
index 95e71d7e612..e66898427f4 100644
--- i/src/include/c.h
+++ w/src/include/c.h
@@ -316,6 +316,23 @@ extern "C++"
 #define pg_attribute_target(...)
 #endif
 
+/*
+ * pg_attribute_strict_flex_array allows specifying the level of strictness for
+ * an individual flexible array member. For instance, most of the time we should
+ * use FLEXIBLE_ARRAY_MEMBER when defining flexible arrays in structs, but
+ * sometimes those structs are embedded within other structs that may not allow
+ * us to do that. pg_attribute_strict_flex_array is a backdoor to lessen
+ * strictness for the singular member such that we can define a flexible array
+ * member using other idioms.
+ *
+ * Currently only available on GCC 15+.
+ */
+#if __has_attribute (strict_flex_array)
+#define pg_attribute_strict_flex_array(level) __attribute__((strict_flex_array(level)))
+#else
+#define pg_attribute_strict_flex_array(level)
+#endif
+
 /*
  * pg_attribute_counted_by specifies that a flexible array member is "counted
  * by" another struct member.  This allows the compiler to improve detection

Reply via email to