- ALM used : GitHub
- CI system used: GitHub actions
- Scanner command used when applicable:
sonar-scanner -Dsonar.projectVersion=1.35.10 -X -Dsonar.pullrequest.key=XXXX -Dsonar.pullrequest.base=XXXX -Dsonar.pullrequest.branch=XXXX -Dsonar.cfamily.gcov.reportsPath=XXXX -Dsonar.cfamily.compile-commands=XXXX/compile_commands.json
- Languages of the repository: C++
- Error observed: we have a case similar to this:
#include <iostream>
struct Foo
{
std::string m_a;
template <class T, class... Args>
void parseArgs(T &&t, Args &&...args) {
m_a += std::to_string(std::forward<T>(t));
parseArgs(std::forward<Args>(args)...);
}
void parseArgs() const {
// The end of template recursion
}
template <class... Args>
explicit Foo(Args &&...args)
{
parseArgs(std::forward<Args>(args)...);
}
};
struct Boo : public Foo
{
int m_b;
explicit Boo(int b) : Foo{m_b, b}, m_b{b} {}
};
int main()
{
Boo boo(12);
std::cout << " " << boo.m_b << std::endl;
std::cout << " " << boo.m_a << std::endl;
return 0;
}
where Boo
inherits from a class that has a generic constructor, and Boo
uses a member variable that has not been initialized to build a string inside Foo
(in this case m_b
), and Sonar is not able to detect this issue.
Notice that if Foo uses the variable to initialize a variable directly, Sonar will complain about assigning a variable to garbage or undefined:
#include <iostream>
struct Foo
{
int m_c;
template <class... Args>
explicit Foo(Args &&...args): m_c(std::forward<Args>(args)...)
{
}
};
struct Boo : public Foo
{
int m_b;
explicit Boo(int b) : Foo{m_b}, m_b{b} {}
};
int main()
{
Boo boo(12);
return 0;
}
Is there a workaround or a better way so that Sonar detects the issue?
You can see the example in godbolt: Compiler Explorer