postgres.git / summary / log / commit / refs
commit a936ebd9f9f64b0efb853195d8dcdb8a0453537d
Author: Peter Eisentraut <peter@eisentraut.org>
Date: Mon Sep 28 07:34:46 2026 +0000
Add pg_attribute_counted_by()
The counted_by attribute allows specifying that a flexible array
member of a struct is "counted by" another member of the same struct.
The attribute makes arrays a little more self-documenting, but it is
also a hint to the compiler such that it can improve detection of
object size information and provide better results in compile-time
diagnostics and run-time features, such as the array bounds sanitizer.
Both GCC and clang warn when the attribute is used in C++, where it is
ignored, so define it away there.
In its current form, it is best used for flexible array members only.
Pointer array support may come later on.
This commit only contains a single use of the attribute for testing.
More will be added afterwards.
Author: Tristan Partin <tristan@partin.io>
Reviewed-by: Peter Eisentraut <peter@eisentraut.org>
Reviewed-by: Japin Li <japinli@hotmail.com>
Reviewed-by: Greg Burd <greg@burd.me>
Discussion: https://www.postgresql.org/message-id/flat/DKBEBF32QLUA.3L2DJRPUYNKHZ%40partin.io
src/backend/utils/adt/jsonfuncs.c | 2 +-
src/include/c.h | 24 ++++++++++++++++++++++++
2 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index bc7b556e22e..64c85e9ddd1 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -233,7 +233,7 @@ struct RecordIOData
Oid record_type;
int32 record_typmod;
int ncolumns;
- ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER];
+ ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(ncolumns);
};
/* per-query cache for populate_record_worker and populate_recordset_worker */
diff --git a/src/include/c.h b/src/include/c.h
index 3b75f0b8610..57d891e52a7 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -297,6 +297,30 @@ extern "C++"
#define pg_attribute_target(...)
#endif
+/*
+ * pg_attribute_counted_by specifies that a flexible array member is "counted
+ * by" another struct member. This allows the compiler to improve detection
+ * of object size information and to provide better results in compile-time
+ * diagnostics and run-time features, such as the array bounds sanitizer.
+ *
+ * Using this annotation comes with additional responsibilities:
+ *
+ * - The count must be assigned before the first reference to the array.
+ * - The array must have at least count elements available at all times,
+ * including after either member is updated.
+ *
+ * The attribute is ignored in C++ due to lack of compiler support.
+ */
+#ifndef __cplusplus
+#if __has_attribute (counted_by)
+#define pg_attribute_counted_by(count) __attribute__((counted_by(count)))
+#else
+#define pg_attribute_counted_by(count)
+#endif
+#else
+#define pg_attribute_counted_by(count)
+#endif
+
/*
* Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
* used in assert-enabled builds, to avoid compiler warnings about unused
[parent: 5bd2e236e21b]