mirror of
https://github.com/postgres/postgres.git
synced 2026-10-23 13:29:09 +03:00
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
This commit is contained in:
@@ -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 */
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user