I’m using SonarQube 8.5 to analyze our projects with C code.
The problem and the example code I met showed below.
If i defined two function in the same file
test1.c :
struct test
{
int i;
char c;
}
void fun(struct test* t)
{
if (test->i == 0) << bug report with garbage value used
//do something
}
void fun2()
{
struct test t;
fun(&t);
}
SonarQube will show the garbage value used in fun()
But if I called the structure in the different file as below
test1.c :
struct test
{
int i;
char c;
}
void fun(struct test* t)
{
if (test->i == 0) << This line will not cause a bug report
//do something
}
test2.c :
void fun2()
{
strct test t;
fun(&t); << niether this line.
}
To investigate this further, I would need a reproducer for the false negative (the issue that is not pointed out). Would you be able to do that?
To generate a reproducer file:
Add the reproducer option to the scanner configuration: sonar.cfamily.reproducer= "Full path to the .cpp file that has or include the file that has the false-negative"
Re-running the scanner should generate a file named sonar-cfamily.reproducer in the project folder.
Please share this file. If you think this file contains private information I can send you a private message to share it
You just need to put the path to the file where you expect an error.
If this error is missing in two files, you can create two different reproducers but I think one will be enough.
By the way, in the second case of your example, are the two files (test1.c) and (test2.c) in the same translation unit? If not, that would explain why we don’t raise an issue: our symbolic execution engine can work cross-files but not cross-translation units. That’s a limitation we have.
The sonar.cfamily.reproducer property was set but no matching file was found. The property was set to:
"/home/sonar/source/test/projects/sources/MY_PROJECT_FILE.cpp"
What do you mean “in the same translation unit”?
In my understanding, every translation unit results in an object file.
Do you mean if I build these two .cpp files to a single object file then it would point out the error?
Thanks for the new example, it allowed me to better understand the problem.
To answer your question: yes, that’s exactly what I meant by the limitation.
Basically what happens is the following:
If the function is defined in the same .c file (or directly in the header .h included in the .c file), we are able to inline everything and check correctly the paths so we raise an issue.
But if the function is defined in another .c file, we don’t have access to the body of this function during the symbolic execution so we don’t raise an issue.