Skip to content

Commit 8254b7e

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 209a22d commit 8254b7e

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
@@ -270,9 +270,6 @@ compareJsonbContainers(JsonbContainer *a, JsonbContainer *b)
270270
else
271271
{
272272
/*
273-
* It's safe to assume that the types differed, and that the va
274-
* and vb values passed were set.
275-
*
276273
* If the two values were of the same container type, then there'd
277274
* have been a chance to observe the variation in the number of
278275
* elements/pairs (when processing WJB_BEGIN_OBJECT, say). They're
@@ -840,15 +837,20 @@ JsonbIteratorInit(JsonbContainer *container)
840837
* It is our job to expand the jbvBinary representation without bothering them
841838
* with it. However, clients should not take it upon themselves to touch array
842839
* or Object element/pair buffers, since their element/pair pointers are
843-
* garbage. Also, *val will not be set when returning WJB_END_ARRAY or
844-
* WJB_END_OBJECT, on the assumption that it's only useful to access values
845-
* when recursing in.
840+
* garbage.
841+
*
842+
* *val is not meaningful when the result is WJB_DONE, WJB_END_ARRAY or
843+
* WJB_END_OBJECT. However, we set val->type = jbvNull in those cases,
844+
* so that callers may assume that val->type is always well-defined.
846845
*/
847846
JsonbIteratorToken
848847
JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
849848
{
850849
if (*it == NULL)
850+
{
851+
val->type = jbvNull;
851852
return WJB_DONE;
853+
}
852854

853855
/*
854856
* When stepping into a nested container, we jump back here to start
@@ -886,6 +888,7 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
886888
* nesting).
887889
*/
888890
*it = freeAndGetParent(*it);
891+
val->type = jbvNull;
889892
return WJB_END_ARRAY;
890893
}
891894

@@ -939,6 +942,7 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
939942
* of nesting).
940943
*/
941944
*it = freeAndGetParent(*it);
945+
val->type = jbvNull;
942946
return WJB_END_OBJECT;
943947
}
944948
else
@@ -983,8 +987,10 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
983987
return WJB_VALUE;
984988
}
985989

986-
elog(ERROR, "invalid iterator state");
987-
return -1;
990+
elog(ERROR, "invalid jsonb iterator state");
991+
/* satisfy compilers that don't know that elog(ERROR) doesn't return */
992+
val->type = jbvNull;
993+
return WJB_DONE;
988994
}
989995

990996
/*

0 commit comments

Comments
 (0)