Skip to content

Commit 0b6dfce

Browse files
committed
Silence uninitialized-value warnings in compareJsonbContainers().
Because not every path through JsonbIteratorNext() sets val->type, some compilers complain that compareJsonbContainers() is comparing possibly-uninitialized values. The paths that don't set it return WJB_DONE, WJB_END_ARRAY, or WJB_END_OBJECT, so it's clear by manual inspection that the "(ra == rb)" code path is safe, and indeed we aren't seeing warnings about that. But the (ra != rb) case is much less obviously safe. In Assert-enabled builds it seems that the asserts rejecting WJB_END_ARRAY and WJB_END_OBJECT persuade gcc 15.x not to warn, which makes little sense because it's impossible to believe that the compiler can prove of its own accord that ra/rb aren't WJB_DONE here. (In fact they never will be, so the code isn't wrong, but why is there no warning?) Without Asserts, the appearance of warnings is quite unsurprising. We discussed fixing this by converting those two Asserts into pg_assume, but that seems not very satisfactory when it's so unclear why the compiler is or isn't warning: the warning could easily reappear with some other compiler version. Let's fix it in a less magical, more future-proof way by changing JsonbIteratorNext() so that it always does set val->type. The cost of that should be pretty negligible, and it makes the function's API spec less squishy. Reported-by: Erik Rijkers <er@xs4all.nl> Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/988bf1bc-3f1f-99f3-bf98-222f1cd9dc5e@xs4all.nl Discussion: https://postgr.es/m/0c623e8a204187b87b4736792398eaf1@postgrespro.ru Backpatch-through: 13
1 parent c33e55a commit 0b6dfce

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/backend/utils/adt/jsonb_util.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,6 @@ compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
277277
else
278278
{
279279
/*
280-
* It's safe to assume that the types differed, and that the va
281-
* and vb values passed were set.
282-
*
283280
* If the two values were of the same container type, then there'd
284281
* have been a chance to observe the variation in the number of
285282
* elements/pairs (when processing WJB_BEGIN_OBJECT, say). They're
@@ -852,15 +849,20 @@ JsonbIteratorInit(JsonbContainer *container)
852849
* It is our job to expand the jbvBinary representation without bothering them
853850
* with it. However, clients should not take it upon themselves to touch array
854851
* or Object element/pair buffers, since their element/pair pointers are
855-
* garbage. Also, *val will not be set when returning WJB_END_ARRAY or
856-
* WJB_END_OBJECT, on the assumption that it's only useful to access values
857-
* when recursing in.
852+
* garbage.
853+
*
854+
* *val is not meaningful when the result is WJB_DONE, WJB_END_ARRAY or
855+
* WJB_END_OBJECT. However, we set val->type = jbvNull in those cases,
856+
* so that callers may assume that val->type is always well-defined.
858857
*/
859858
JsonbIteratorToken
860859
JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
861860
{
862861
if (*it == NULL)
862+
{
863+
val->type = jbvNull;
863864
return WJB_DONE;
865+
}
864866

865867
/*
866868
* When stepping into a nested container, we jump back here to start
@@ -898,6 +900,7 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
898900
* nesting).
899901
*/
900902
*it = freeAndGetParent(*it);
903+
val->type = jbvNull;
901904
return WJB_END_ARRAY;
902905
}
903906

@@ -951,6 +954,7 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
951954
* of nesting).
952955
*/
953956
*it = freeAndGetParent(*it);
957+
val->type = jbvNull;
954958
return WJB_END_OBJECT;
955959
}
956960
else
@@ -995,8 +999,10 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
995999
return WJB_VALUE;
9961000
}
9971001

998-
elog(ERROR, "invalid iterator state");
999-
return -1;
1002+
elog(ERROR, "invalid jsonb iterator state");
1003+
/* satisfy compilers that don't know that elog(ERROR) doesn't return */
1004+
val->type = jbvNull;
1005+
return WJB_DONE;
10001006
}
10011007

10021008
/*

0 commit comments

Comments
 (0)