pgbackrest.git / summary / log / commit / refs

commit    bb7e1724a95a4bf087bae6782ae255e1583bc9b7
Author:   David Steele <david@pgbackrest.org>
Date:     Mon Oct 28 11:21:35 2024 +0000

    Update String, Buffer, and Variant constants to work with cast-qual.
    
    These three objects can be created as constants at compile time using specialized macros. Unfortunately since the values assigned are also const, cast-qual complained about the cost qualifier being lost.
    
    Fix this by creating new structures to be used just for creating these constants. This is not ideal due to the need to keep the duplicated structures in sync, but in practice these structures are almost never modified. Testing should catch any out of sync structures and this feature is valuable enough to keep even though in theory there could be memory safety issues. In practice the APIs prevent const objects from being used in an unsafe way and testing provides a fair assurance of safety. Writing to these consts would be a fatal error even if it did not cause a segfault.
    
    Ideally, we would be able to use warning suppression in these macros to avoid the extra struct, but due to the way they are used it is not possible to add the required pragmas (even using _Pragma).
    
    Finally this construction makes it obvious that something special is being done, rather than it being under the covers.


src/build/config/render.c | 3 ++- src/common/type/buffer.h | 18 ++++++++++++++---- src/common/type/string.h | 10 +++++++++- src/common/type/variant.h | 15 +++++++++++---- src/config/parse.auto.c.inc | 2 +- src/config/parse.c | 2 +- test/src/module/build/configTest.c | 2 +- 7 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/build/config/render.c b/src/build/config/render.c index abfd2afed..8fbd24bfa 100644 --- a/src/build/config/render.c +++ b/src/build/config/render.c @@ -1247,7 +1247,8 @@ bldCfgRenderParseAutoC(const Storage *const storageRepo, const BldCfg bldCfg, co String *const configVal = strNew(); strCat( - configVal, bldCfgRenderValueRender(OPT_TYPE_STRING_STR, ruleValMap, label, "StringPub", "String", "Str", "STR", "val/str")); + configVal, + bldCfgRenderValueRender(OPT_TYPE_STRING_STR, ruleValMap, label, "StringPubConst", "String", "Str", "STR", "val/str")); strCat( configVal, bldCfgRenderValueRender(OPT_TYPE_STRING_ID_STR, ruleValMap, label, "StringId", "StringId", "StrId", "STRID", "val/strid")); diff --git a/src/common/type/buffer.h b/src/common/type/buffer.h index 8f83d1e71..5c4f49c69 100644 --- a/src/common/type/buffer.h +++ b/src/common/type/buffer.h @@ -174,21 +174,31 @@ will result in a segfault. By convention all buffer constant identifiers are appended with _BUF. ***********************************************************************************************************************************/ +// This struct must be kept in sync with BufferPub (except for const qualifiers) +typedef struct BufferPubConst +{ + size_t sizeAlloc; // Allocated size of the buffer + size_t size; // Reported size of the buffer + bool sizeLimit; // Is the size limited to make the buffer appear smaller? + size_t used; // Amount of buffer used + const unsigned char *buffer; // Buffer +} BufferPubConst; + // Create a buffer constant inline from an unsigned char[] #define BUF(bufferParam, sizeParam) \ - ((const Buffer *)&(const BufferPub){.size = sizeParam, .used = sizeParam, .buffer = (unsigned char *)bufferParam}) + ((const Buffer *)&(const BufferPubConst){.size = sizeParam, .used = sizeParam, .buffer = (const unsigned char *)bufferParam}) // Create a buffer constant inline from a non-constant zero-terminated string #define BUFSTRZ(stringz) \ - BUF((unsigned char *)stringz, strlen(stringz)) + BUF((const unsigned char *)stringz, strlen(stringz)) // Create a buffer constant inline from a String #define BUFSTR(string) \ - BUF((unsigned char *)strZ(string), strSize(string)) + BUF((const unsigned char *)strZ(string), strSize(string)) // Create a buffer constant inline from a constant zero-terminated string #define BUFSTRDEF(stringdef) \ - BUF((unsigned char *)stringdef, (sizeof(stringdef) - 1)) + BUF((const unsigned char *)stringdef, (sizeof(stringdef) - 1)) // Used to define buffer constants that will be externed using BUFFER_DECLARE(). Must be used in a .c file. #define BUFFER_EXTERN(name, ...) \ diff --git a/src/common/type/string.h b/src/common/type/string.h index f2cd57e22..1f3769c64 100644 --- a/src/common/type/string.h +++ b/src/common/type/string.h @@ -244,8 +244,16 @@ will result in a segfault due to modifying read-only memory. By convention all string constant identifiers are appended with _STR. ***********************************************************************************************************************************/ +// This struct must be kept in sync with StringPub (except for const qualifiers) +typedef struct StringPubConst +{ + uint64_t size : 32; // Actual size of the string + uint64_t extra : 32; // Extra space allocated for expansion + const char *buffer; // String buffer +} StringPubConst; + #define STR_SIZE(bufferParam, sizeParam) \ - ((const String *const)&(const StringPub){.buffer = (char *)(bufferParam), .size = (unsigned int)(sizeParam)}) + ((const String *const)&(const StringPubConst){.buffer = (const char *const)(bufferParam), .size = (unsigned int)(sizeParam)}) // Create a String constant inline from any zero-terminated string #define STR(buffer) \ diff --git a/src/common/type/variant.h b/src/common/type/variant.h index f19ecc1ce..882fcd521 100644 --- a/src/common/type/variant.h +++ b/src/common/type/variant.h @@ -179,17 +179,24 @@ By convention all variant constant identifiers are appended with _VAR. #define VARINT64(dataParam) \ ((const Variant *)&(const VariantInt64Pub){.type = varTypeInt64, .data = dataParam}) -// Create a String Variant constant inline from any zero-terminated string +// Create a String Variant constant inline from any zero-terminated string. The struct must be kept in sync with VariantStringPub +// (except for const qualifiers) +typedef struct VariantStringPubConst +{ + VARIANT_COMMON + const String *data; // String data +} VariantStringPubConst; + #define VARSTRZ(dataParam) \ - ((const Variant *)&(const VariantStringPub){.type = varTypeString, .data = (String *)STR(dataParam)}) + ((const Variant *)&(const VariantStringPubConst){.type = varTypeString, .data = (const String *)STR(dataParam)}) // Create a String Variant constant inline from a #define or inline string constant #define VARSTRDEF(dataParam) \ - ((const Variant *)&(const VariantStringPub){.type = varTypeString, .data = (String *)STRDEF(dataParam)}) + ((const Variant *)&(const VariantStringPubConst){.type = varTypeString, .data = (const String *)STRDEF(dataParam)}) // Create a String Variant constant inline from a String constant #define VARSTR(dataParam) \ - ((const Variant *)&(const VariantStringPub){.type = varTypeString, .data = (String *)(dataParam)}) + ((const Variant *)&(const VariantStringPubConst){.type = varTypeString, .data = (const String *)(dataParam)}) // Used to define String Variant constants that will be externed using VARIANT_DECLARE(). Must be used in a .c file. #define VARIANT_STRDEF_EXTERN(name, dataParam) \ diff --git a/src/config/parse.auto.c.inc b/src/config/parse.auto.c.inc index 37ef975dc..5faee9ed1 100644 --- a/src/config/parse.auto.c.inc +++ b/src/config/parse.auto.c.inc @@ -9,7 +9,7 @@ Rule Strings ***********************************************************************************************************************************/ #define PARSE_RULE_VAL_STR(value) PARSE_RULE_U32_1(parseRuleValStr##value) -static const StringPub parseRuleValueStr[] = +static const StringPubConst parseRuleValueStr[] = { PARSE_RULE_STRPUB("/tmp/pgbackrest"), // val/str PARSE_RULE_STRPUB("/var/lib/pgbackrest"), // val/str diff --git a/src/config/parse.c b/src/config/parse.c index 3c5805e13..d97061aae 100644 --- a/src/config/parse.c +++ b/src/config/parse.c @@ -213,7 +213,7 @@ typedef enum #define PARSE_RULE_OPTION_COMMAND(commandParam) \ | (1 << cfgCmd##commandParam) -#define PARSE_RULE_STRPUB(value) {.buffer = (char *)value, .size = sizeof(value) - 1} +#define PARSE_RULE_STRPUB(value) {.buffer = (const char *)value, .size = sizeof(value) - 1} // Macros used to define optional parse rules in pack format #define PARSE_RULE_VARINT_01(value) \ diff --git a/test/src/module/build/configTest.c b/test/src/module/build/configTest.c index 8009600ff..db1517560 100644 --- a/test/src/module/build/configTest.c +++ b/test/src/module/build/configTest.c @@ -600,7 +600,7 @@ testRun(void) COMMENT_BLOCK_END "\n" "#define PARSE_RULE_VAL_STR(value) PARSE_RULE_U32_1(parseRuleValStr##value)\n" "\n" - "static const StringPub parseRuleValueStr[] =\n" + "static const StringPubConst parseRuleValueStr[] =\n" "{\n" " PARSE_RULE_STRPUB(\"/include\"),\n" " PARSE_RULE_STRPUB(\"0\"),\n" [parent: a981ef08d70f]