Skip to content

Commit

Permalink
Silence invalid Coverity complaint in jsonReadPush().
Browse files Browse the repository at this point in the history
Coverity complains that the output from THROW_FMT will be unpredictable since the order of operations in the call is not deterministic, but it fails to understand that subsequent calls to jsonReadTypeNextIgnoreComma() are noops until the value has been processed.

Silence Coverity by assigning the actual type to a local variable so jsonReadTypeNextIgnoreComma() is only called once.

Also fix an adjacent comment typo.
  • Loading branch information
dwsteele committed May 10, 2024
1 parent bb8988b commit d32eb5b
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/common/type/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,19 @@ jsonReadPush(JsonRead *const this, const JsonType type, const bool key)
if (this->complete)
THROW(FormatError, "JSON read is complete");

// Check that the requested type matches the actual type
if (jsonReadTypeNextIgnoreComma(this) != type)
// Check that the requested type matches the actual type. The actual type needs to be assigned to a local variable or Coverity
// will complain about parameter order when jsonReadTypeNextIgnoreComma() is called again in THROW_FMT() even though it is a
// noop.
const JsonType typeActual = jsonReadTypeNextIgnoreComma(this);

if (typeActual != type)
{
THROW_FMT(
JsonFormatError, "expected '%s' but found '%s' at: %s", strZ(strIdToStr(type)),
strZ(strIdToStr(jsonReadTypeNextIgnoreComma(this))), this->json);
JsonFormatError, "expected '%s' but found '%s' at: %s", strZ(strIdToStr(type)), strZ(strIdToStr(typeActual)),
this->json);
}

// If the container stack jas not been created yet
// If the container stack has not been created yet
if (this->stack == NULL)
{
ASSERT(!key);
Expand Down

0 comments on commit d32eb5b

Please sign in to comment.