Reputation: 443
I am trying to avoid C6011 warning because an abort function is calling exit(). How can I do that?
Here is an example:
#include <stdlib.h>
void abort_function();
void func( int *p );
int main()
{
int x;
func( &x );
return 0;
}
void func( int *p )
{
if (NULL == p)
abort_function();
*p = 5;
}
void abort_function()
{
exit(0);
}
So this leads to the following warning from PREFast:
warning C6011: Dereferencing NULL pointer 'p': Lines: 17, 18, 20
Simply replacing abort_function()
with exit(0)
eliminates this warning.
But I'm actually working with a large codebase, and I didn't want to replace all calls to abort_function()
. So I was able to eliminate a lot of these warnings by making the function a variadic macro, and temporarily taking out the function definition, like this:
#include <stdlib.h>
#define abort_function( ... ) exit(0);
/*void abort_function();*/
void func( int *p );
int main()
{
int x;
func( &x );
return 0;
}
void func( int *p )
{
if (NULL == p)
abort_function();
*p = 5;
}
#if 0
void abort_function()
{
exit(0);
}
#endif
This also eliminated the warning, but are there any PREFast options or annotations I could use, to avoid having to modify the code?
Upvotes: 4
Views: 3030
Reputation: 16441
In MSVC, defining __declspec(noreturn)
for abort_function
should do it.
For gcc, __attribute__ ((noreturn))
does the same.
Upvotes: 9
Reputation: 973
The reason you are getting this warning is because the compiler doesn't realize that abort_function() will never return. Therefore it believes that there is a legitimate code path where p is NULL; abort_function() is called and returns and then you dereference p.
You can avoid it by either using the (noreturn) declaration or modifying your abort_function() invocation to include a return;
if (p == NULL)
{
abort_function();
return;
}
Upvotes: 2
Reputation: 836
In C, you can eliminate warnings like this using #pragma. Use it carefully, because you want some warnings. Here is the #pragma for eliminating this warning:
#pragma warning(disable:6011)
Upvotes: 2