Skip to content

Commit 5a2139a

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 bdb052f commit 5a2139a

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
@@ -845,15 +842,20 @@ JsonbIteratorInit(JsonbContainer *container)
845842
* It is our job to expand the jbvBinary representation without bothering them
846843
* with it. However, clients should not take it upon themselves to touch array
847844
* or Object element/pair buffers, since their element/pair pointers are
848-
* garbage. Also, *val will not be set when returning WJB_END_ARRAY or
849-
* WJB_END_OBJECT, on the assumption that it's only useful to access values
850-
* when recursing in.
845+
* garbage.
846+
*
847+
* *val is not meaningful when the result is WJB_DONE, WJB_END_ARRAY or
848+
* WJB_END_OBJECT. However, we set val->type = jbvNull in those cases,
849+
* so that callers may assume that val->type is always well-defined.
851850
*/
852851
JsonbIteratorToken
853852
JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
854853
{
855854
if (*it == NULL)
855+
{
856+
val->type = jbvNull;
856857
return WJB_DONE;
858+
}
857859

858860
/*
859861
* When stepping into a nested container, we jump back here to start
@@ -891,6 +893,7 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
891893
* nesting).
892894
*/
893895
*it = freeAndGetParent(*it);
896+
val->type = jbvNull;
894897
return WJB_END_ARRAY;
895898
}
896899

@@ -944,6 +947,7 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
944947
* of nesting).
945948
*/
946949
*it = freeAndGetParent(*it);
950+
val->type = jbvNull;
947951
return WJB_END_OBJECT;
948952
}
949953
else
@@ -988,8 +992,10 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
988992
return WJB_VALUE;
989993
}
990994

991-
elog(ERROR, "invalid iterator state");
992-
return -1;
995+
elog(ERROR, "invalid jsonb iterator state");
996+
/* satisfy compilers that don't know that elog(ERROR) doesn't return */
997+
val->type = jbvNull;
998+
return WJB_DONE;
993999
}
9941000

9951001
/*

0 commit comments

Comments
 (0)