Context
- ALM: GitHub
- Language: C++
- URL: SonarCloud
Observed behavior
Rule cpp:S2259 marks a certain pointer as being accessed while null, but it is not.
Steps to reproduce
I tried to extract the minimal code that should reproduce the issue from our production code, but couldn’t test it directly, please let me know if you have issues reproducing it:
struct DataObject {
int content = 0;
};
struct DataSource {
std::string name;
DataObject value;
};
struct DataTarget {
DataObject* property1 = nullptr;
DataObject* property2 = nullptr;
DataObject* property3 = nullptr;
};
void readAttribute(std::map<std::string, bool>& attributeFound, const DataSource& ds, const std::string& targetName, DataObject*& out) {
if (ds.name != targetName) {
return;
}
if (attributeFound[ds.name]) {
return;
}
out = &ds.value;
attributeFound[ds.name] = true;
}
void demoFunction() {
// This vector is being filled by some other function gethering data in the actual code
// and element order in this vector is undetermined
std::vector<DataSource> datasources = {{"ds_property2", {42}}, {"ds_property3", {43}}, {"ds_property1", {41}}};
std::map<std::string, bool> attributeFound = {
{"ds_property1", false},
{"ds_property2", false},
{"ds_property3", false},
};
DataTarget dataTarget;
// Browse all data sources and try to find the properties we need
for (const DataSource& ds : datasources)
{
readAttribute(attributeFound, ds, "ds_property1", dataTarget.property1);
readAttribute(attributeFound, ds, "ds_property2", dataTarget.property2);
readAttribute(attributeFound, ds, "ds_property3", dataTarget.property3);
}
if (attributeFound["ds_property1"]) {
// Here cpp:S2259 triggers on dataTarget.property1->content of the line below, which is wrong
std::cout << "Property 1 = " << dataTarget.property1->content << std::endl;
}
}