Hi,
I’m new to the community and the tool, and probably not getting the things right,
but after several similar cases, I finally decided to ask someone.
here I have this code
The bug report is not completely unreasonable, as LoadIcon() may return NULL,
but the message that is shown is definitely not correct. How can hIcon
this be an uninitialized value ?
I’m using SonarCube 9.6.1 (build 59531)
Colin
(Colin)
November 23, 2022, 3:54pm
2
Hey there.
Welcome to our Community! I’ve moved your post to the section for reporting false-postives.
Hey SonarSource Community!
False-positives happen , as do false-negatives, and we’re eager to fix them. We are thrilled when our users report issues so that we can make our products better.
What is a false-positive (FP)?
A false-positive is when an issue is raised unexpectedly on code that should not trigger an issue, or where the suggested action doesn’t make any sense for the code.
What is a false-negative (FN)?
A false-negative is when an issue should be raised on a piece of code, but isn…
Please update your thread with a text-based code sample that reproduces the issue (not a screenshot)
I did some investigation work. It looks that if the RHS of the expression is a macro, the assignment is not detected by the software.
LoadIcon is a macro that redirects to either LoadIconA or LoadIconW like that:
#ifdef UNICODE
#define LoadIcon LoadIconW
#else
#define LoadIcon LoadIconA
#endif // !UNICODE
and then the actual code that reproduces the behavior
HICON hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON1));
//reports cpp:S836: "1st function call argument is an uninitialized value"
SetIcon(hIcon, TRUE);
there is another trigger to the same behavior - a templated function call
class myClass
{
public:
template<UINT A, UINT B> static const char *ID2Type(UINT nID);
};
template<UINT A, UINT B> inline const char *myClass::ID2Type(UINT nID)
{
switch (nID)
{
case A: return "TypeA";
case B: return "TypeB";
default: return nullptr;
}
}
void main()
{
const char *type = myClass::ID2Type<1,2>(1);
// reports cpp:S836 "Branch condition evaluates to a garbage value"
if(!type)
return 1;
return 0;
}
mickdo
(Michael Andrews)
September 7, 2023, 2:40am
4
Any updates on this?
We are seeing many similar false positives where a macro is seemingly not being expanded by the scan.
This is quite a big limitation?
Colin
(Colin)
September 8, 2023, 7:42am
5
It seems this fell off my plate at the beginning of the year.
Hey @mickdo
What version of SonarQube are you using? Can you share an example like @Nikolay_Bakalov ?
mickdo
(Michael Andrews)
September 8, 2023, 8:46am
6
Hi Colin,
actually it looks like the issue was down to some of the compiler includes not being correct for the build wrapper (my fault, not a SonarQube problem).
The includes were distant from the macros in question, but it seemed to be enough to throw off the scan and generate some spurious false positives there.
All good now! Thanks for the quick response
1 Like