Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid invalid warning C6011 due to an abort function calling exit()

Tags:

c

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?

like image 246
Rich Jahn Avatar asked Dec 20 '22 20:12

Rich Jahn


2 Answers

In MSVC, defining __declspec(noreturn) for abort_function should do it.
For gcc, __attribute__ ((noreturn)) does the same.

like image 157
ugoren Avatar answered Dec 23 '22 10:12

ugoren


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)
like image 29
MYMNeo Avatar answered Dec 23 '22 11:12

MYMNeo