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?
In MSVC, defining __declspec(noreturn)
for abort_function
should do it.
For gcc, __attribute__ ((noreturn))
does the same.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With