Skip to content

Commit f32e456

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 5ea9d9a commit f32e456

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

854856
/*
855857
* When stepping into a nested container, we jump back here to start
@@ -887,6 +889,7 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
887889
* nesting).
888890
*/
889891
*it = freeAndGetParent(*it);
892+
val->type = jbvNull;
890893
return WJB_END_ARRAY;
891894
}
892895

@@ -940,6 +943,7 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
940943
* of nesting).
941944
*/
942945
*it = freeAndGetParent(*it);
946+
val->type = jbvNull;
943947
return WJB_END_OBJECT;
944948
}
945949
else
@@ -984,8 +988,10 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
984988
return WJB_VALUE;
985989
}
986990

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

991997
/*

0 commit comments

Comments
 (0)