Imagine there is a code like:
#include "stdio.h"
int divide(int a, int b, float *res)
{
if (b == 0)
return 0;
*res = (float)a/b;
return 1;
}
int main()
{
float res;
divide(2,5,&res);
printf("%f", res);
return 0;
}
The obvious mistake here is that the return of divide()
is not being checked before printing the result.
Is there a way to detect such error automatically?
(like script, tool or service)
It would be very useful for a large codebase. I have not found any gcc
or clang
flags that could help. cppcheck
also seems not to offer such feature.