Open cschreib-ibex opened 1 week ago
For people coming here with the same problem, this cannot be solved with // NOLINT
commands, since this is not coming from clang-tidy
but from the clang
builtin analyzer. The only way that I know to silence these false positives is to add a pre-processor compilation guard, such as:
#include "jsoncons/json.hpp"
void test(jsoncons::json some_json, const std::string &some_string) {
#if !defined(__clang_analyzer__)
// The normal code.
some_json["test"] = some_string;
#else
// The code that the analyzer will see.
// Need so silence unused variable warnings...
static_cast<void>(some_json);
static_cast<void>(some_string);
#endif
}
Describe the proposed feature We use jsoncons in one of our project, for which we run clang static analysis on every build. Unfortunately, jsoncons has had a history of triggering false positives from clang's compile-time memory leak analyzer, and this has gotten worse in 0.178.0 (we just upgraded from 0.176.0).
What other libraries (C++ or other) have this feature?
nlohmann::json
Include a code fragment with sample data that illustrates the use of this feature Here's an example that triggers the false positive:
Here's the
compile_commands.json
file (adjust with your own paths):Running
clang-tidy-16 -p . test.cpp
, we get the report shown in full in "Details" below. In summary, the analyzer is unable to keep track of the storage kind of a JSON variable across function calls; it clearly sees an JSON variable being initialised as an "object" (and sees the memory allocated for that), and then immediately after it goes down an unreachableswitch
case where the variable is a "string" (which thus fails to de-allocate the memory allocated for the object). I don't know if something can be done to help analysers track this better.This example reports no leak with
nlohmann::json
.