Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaad1617

Browse files
committed
Silence uninitialized-value warnings in compareJsonbContainers().
Because not every path through JsonbIteratorNext() sets val->type,some compilers complain that compareJsonbContainers() is comparingpossibly-uninitialized values. The paths that don't set it returnWJB_DONE, WJB_END_ARRAY, or WJB_END_OBJECT, so it's clear bymanual inspection that the "(ra == rb)" code path is safe, andindeed we aren't seeing warnings about that. But the (ra != rb)case is much less obviously safe. In Assert-enabled builds itseems that the asserts rejecting WJB_END_ARRAY and WJB_END_OBJECTpersuade gcc 15.x not to warn, which makes little sense becauseit's impossible to believe that the compiler can prove of itsown accord that ra/rb aren't WJB_DONE here. (In fact they neverwill 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 intopg_assume, but that seems not very satisfactory when it's so unclearwhy the compiler is or isn't warning: the warning could easilyreappear with some other compiler version. Let's fix it in a lessmagical, more future-proof way by changing JsonbIteratorNext()so that it always does set val->type. The cost of that should bepretty 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.nlDiscussion:https://postgr.es/m/0c623e8a204187b87b4736792398eaf1@postgrespro.ruBackpatch-through: 13
1 parent8ffd9ac commitaad1617

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,boolskipNested)
861860
{
862861
if (*it==NULL)
862+
{
863+
val->type=jbvNull;
863864
returnWJB_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
returnWJB_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
returnWJB_END_OBJECT;
955959
}
956960
else
@@ -995,8 +999,10 @@ JsonbIteratorNext(JsonbIterator **it, JsonbValue *val, bool skipNested)
995999
returnWJB_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+
returnWJB_DONE;
10001006
}
10011007

10021008
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp