Hi,It seems that not all parameters passed by pointer to static functions defined in src/backend/utils/adt/jsonb_util.c are actually modified by these functions. I propose a simple patch that adds const modifiers to such parameters to make it clear that their values remain intact during the call.
This modification was inspired by warnings produced by GCC 15.1 for jsonb_util.c, discovered independently, but, as it turned out, already described in this thread: https://postgrespro.com/list/id/988bf1bc-3f1f-99f3-bf98-222f1cd9d...@xs4all.nl .
This is my first submission so I'm sorry in advance for any mistakes. This modification was passed through pgindent, clearly compiles with GCC 15.1.1 on Arch Linux x86 64-bit, and ‘make check’ successfully passes for it. I'm attaching the patch.
Best regards, Dmitry
From: Dmitry Mityugov <d.mityu...@postgrespro.ru> Date: Mon, 7 Jul 2025 17:59:49 +0300 Subject: Add const to static functions in jsonb_utils.c Not all parameters passed by pointer to static functions defined in backend/utils/adt/jsonb_util.c are actually modified by these functions. This patch adds const modifier to such parameters to make it clear that their values remain constant during the call. --- src/backend/utils/adt/jsonb_util.c | 48 +++++++++++++++--------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/backend/utils/adt/jsonb_util.c b/src/backend/utils/adt/jsonb_util.c index c8b6c15e059..c18bf741521 100644 --- a/src/backend/utils/adt/jsonb_util.c +++ b/src/backend/utils/adt/jsonb_util.c @@ -36,16 +36,16 @@ #define JSONB_MAX_ELEMS (Min(MaxAllocSize / sizeof(JsonbValue), JB_CMASK)) #define JSONB_MAX_PAIRS (Min(MaxAllocSize / sizeof(JsonbPair), JB_CMASK)) -static void fillJsonbValue(JsonbContainer *container, int index, +static void fillJsonbValue(const JsonbContainer *container, int index, char *base_addr, uint32 offset, JsonbValue *result); -static bool equalsJsonbScalarValue(JsonbValue *a, JsonbValue *b); -static int compareJsonbScalarValue(JsonbValue *a, JsonbValue *b); -static Jsonb *convertToJsonb(JsonbValue *val); -static void convertJsonbValue(StringInfo buffer, JEntry *header, JsonbValue *val, int level); -static void convertJsonbArray(StringInfo buffer, JEntry *header, JsonbValue *val, int level); -static void convertJsonbObject(StringInfo buffer, JEntry *header, JsonbValue *val, int level); -static void convertJsonbScalar(StringInfo buffer, JEntry *header, JsonbValue *scalarVal); +static bool equalsJsonbScalarValue(const JsonbValue *a, const JsonbValue *b); +static int compareJsonbScalarValue(const JsonbValue *a, const JsonbValue *b); +static Jsonb *convertToJsonb(const JsonbValue *val); +static void convertJsonbValue(StringInfo buffer, JEntry *header, const JsonbValue *val, int level); +static void convertJsonbArray(StringInfo buffer, JEntry *header, const JsonbValue *val, int level); +static void convertJsonbObject(StringInfo buffer, JEntry *header, const JsonbValue *val, int level); +static void convertJsonbScalar(StringInfo buffer, JEntry *header, const JsonbValue *scalarVal); static int reserveFromBuffer(StringInfo buffer, int len); static void appendToBuffer(StringInfo buffer, const void *data, int len); @@ -55,9 +55,9 @@ static short padBufferToInt(StringInfo buffer); static JsonbIterator *iteratorFromContainer(JsonbContainer *container, JsonbIterator *parent); static JsonbIterator *freeAndGetParent(JsonbIterator *it); static JsonbParseState *pushState(JsonbParseState **pstate); -static void appendKey(JsonbParseState *pstate, JsonbValue *string); -static void appendValue(JsonbParseState *pstate, JsonbValue *scalarVal); -static void appendElement(JsonbParseState *pstate, JsonbValue *scalarVal); +static void appendKey(JsonbParseState *pstate, const JsonbValue *string); +static void appendValue(JsonbParseState *pstate, const JsonbValue *scalarVal); +static void appendElement(JsonbParseState *pstate, const JsonbValue *scalarVal); static int lengthCompareJsonbStringValue(const void *a, const void *b); static int lengthCompareJsonbString(const char *val1, int len1, const char *val2, int len2); @@ -66,7 +66,7 @@ static void uniqueifyJsonbObject(JsonbValue *object, bool unique_keys, bool skip_nulls); static JsonbValue *pushJsonbValueScalar(JsonbParseState **pstate, JsonbIteratorToken seq, - JsonbValue *scalarVal); + const JsonbValue *scalarVal); void JsonbToJsonbValue(Jsonb *jsonb, JsonbValue *val) @@ -509,7 +509,7 @@ getIthJsonbValueFromContainer(JsonbContainer *container, uint32 i) * expanded. */ static void -fillJsonbValue(JsonbContainer *container, int index, +fillJsonbValue(const JsonbContainer *container, int index, char *base_addr, uint32 offset, JsonbValue *result) { @@ -645,7 +645,7 @@ pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, */ static JsonbValue * pushJsonbValueScalar(JsonbParseState **pstate, JsonbIteratorToken seq, - JsonbValue *scalarVal) + const JsonbValue *scalarVal) { JsonbValue *result = NULL; @@ -750,7 +750,7 @@ pushState(JsonbParseState **pstate) * pushJsonbValue() worker: Append a pair key to state when generating a Jsonb */ static void -appendKey(JsonbParseState *pstate, JsonbValue *string) +appendKey(JsonbParseState *pstate, const JsonbValue *string) { JsonbValue *object = &pstate->contVal; @@ -779,7 +779,7 @@ appendKey(JsonbParseState *pstate, JsonbValue *string) * Jsonb */ static void -appendValue(JsonbParseState *pstate, JsonbValue *scalarVal) +appendValue(JsonbParseState *pstate, const JsonbValue *scalarVal) { JsonbValue *object = &pstate->contVal; @@ -792,7 +792,7 @@ appendValue(JsonbParseState *pstate, JsonbValue *scalarVal) * pushJsonbValue() worker: Append an element to state when generating a Jsonb */ static void -appendElement(JsonbParseState *pstate, JsonbValue *scalarVal) +appendElement(JsonbParseState *pstate, const JsonbValue *scalarVal) { JsonbValue *array = &pstate->contVal; @@ -1405,7 +1405,7 @@ JsonbHashScalarValueExtended(const JsonbValue *scalarVal, uint64 *hash, * Are two scalar JsonbValues of the same type a and b equal? */ static bool -equalsJsonbScalarValue(JsonbValue *a, JsonbValue *b) +equalsJsonbScalarValue(const JsonbValue *a, const JsonbValue *b) { if (a->type == b->type) { @@ -1437,7 +1437,7 @@ equalsJsonbScalarValue(JsonbValue *a, JsonbValue *b) * operators, where a lexical sort order is generally expected. */ static int -compareJsonbScalarValue(JsonbValue *a, JsonbValue *b) +compareJsonbScalarValue(const JsonbValue *a, const JsonbValue *b) { if (a->type == b->type) { @@ -1552,7 +1552,7 @@ padBufferToInt(StringInfo buffer) * Given a JsonbValue, convert to Jsonb. The result is palloc'd. */ static Jsonb * -convertToJsonb(JsonbValue *val) +convertToJsonb(const JsonbValue *val) { StringInfoData buffer; JEntry jentry; @@ -1594,7 +1594,7 @@ convertToJsonb(JsonbValue *val) * for debugging purposes. */ static void -convertJsonbValue(StringInfo buffer, JEntry *header, JsonbValue *val, int level) +convertJsonbValue(StringInfo buffer, JEntry *header, const JsonbValue *val, int level) { check_stack_depth(); @@ -1619,7 +1619,7 @@ convertJsonbValue(StringInfo buffer, JEntry *header, JsonbValue *val, int level) } static void -convertJsonbArray(StringInfo buffer, JEntry *header, JsonbValue *val, int level) +convertJsonbArray(StringInfo buffer, JEntry *header, const JsonbValue *val, int level) { int base_offset; int jentry_offset; @@ -1703,7 +1703,7 @@ convertJsonbArray(StringInfo buffer, JEntry *header, JsonbValue *val, int level) } static void -convertJsonbObject(StringInfo buffer, JEntry *header, JsonbValue *val, int level) +convertJsonbObject(StringInfo buffer, JEntry *header, const JsonbValue *val, int level) { int base_offset; int jentry_offset; @@ -1819,7 +1819,7 @@ convertJsonbObject(StringInfo buffer, JEntry *header, JsonbValue *val, int level } static void -convertJsonbScalar(StringInfo buffer, JEntry *header, JsonbValue *scalarVal) +convertJsonbScalar(StringInfo buffer, JEntry *header, const JsonbValue *scalarVal) { int numlen; short padlen; -- 2.50.0