2

While trying to rid my code of GCC warnings I have discovered a strange bug/behaviour. My program uses a custom exit function which performs some clean-up and finally calls exit().

However my use of this exit function is causing GCC to produce spurious warnings of:

control reaches end of non-void function with custom exit function

An example of a program causing this bug is:

File1.c:

extern void CleanExit(int ExitCode);

int MyFunc(int ErrorCode)
{
    if(-1 == ErrorCode)
        return -1;
    if(1 == ErrorCode)
        return 1;
    if(2 == ErrorCode)
        return 0;
    if(3 == ErrorCode)
        CleanExit(0);
    CleanExit(-1);
}

File2.c:

void CleanExit(int ExitCode)
{
    /* Do Some Cleanup */
    exit(ExitCode);
}

Is there some way to be rid of these warnings without adding useless cruft to every function which uses this structure?

I would prefer to do so with standard C but am willing to use some kind of GCC pragma if needed.

3
  • 1
    Did you mean exit(ExitCode) instead of exit(int ExitCode)? And just move the exit() from CleanExit() function to MyFunc() function to get rid of the warning.
    – Spikatrix
    Dec 27, 2014 at 12:55
  • Function MyFunc do not have a return. This already discussed in stackoverflow.com/questions/19669851/…
    – Obik
    Dec 27, 2014 at 12:59
  • @CoolGuy You are correct with the correction, sorry. I messed up while creating a minimal example. Though With regards to moving the exit out, I will do that as a last resort, but am hoping if possible to find a way which does not force me to modify every call to CleanExit, as there are hundreds across many files.
    – Vality
    Dec 27, 2014 at 13:00

3 Answers 3

5

You tagged your question with GCC. So instead of changing function from void to int and adding return everywhere, you can try and GCC specify attribute. Add __attribute__((noreturn)) above CleanExit() declaration. This will alert the compiler that CleanExit() will never return directly to the caller.

2
  • Thanks, this does indeed add a compiler specific line, but given gcc is the only compiler which causes the warning I should be able to easily enough ifdef this and get the right behaviour. Thanks
    – Vality
    Dec 27, 2014 at 13:15
  • If you have a C11 compiler, no need to use an extension. Now there is the keyword _Noreturn that you can use instead. Dec 27, 2014 at 17:11
2

This should do it

extern int CleanExit(int ExitCode);

int MyFunc(int ErrorCode)
{
    if(-1 == ErrorCode)
        return -1;
    if(1 == ErrorCode)
        return 1;
    if(2 == ErrorCode)
        return 0;
    if(3 == ErrorCode)
        return CleanExit(0);
    return CleanExit(-1);
}

int CleanExit(int ExitCode)
{
    /* Do Some Cleanup */
    exit(ExitCode);
}
3
  • but don't you need to return a value in CleanExit?
    – phuclv
    Dec 27, 2014 at 13:01
  • Since you are calling exit inside that function there will be no warning. Dec 27, 2014 at 13:02
  • Downvote without a comment is rude, I would love to know what is wrong with this approach so I learn from you. Dec 27, 2014 at 13:23
-1

Those warnings aren't spurious - there is a difference between returning from a function (and eventually exiting by returning from main) and calling exit() within another function to terminate the entire program. You don't really need to know those differences in this instance, but you can read about them if you'd like.

In this case, the problem is with your function's signature. MyFunc is expected to return an int, but doesn't when CleanExit is called. As a result, the compiler complains about a missing return (but is smart enough to recognize an exit call and note it in the place of a return).

To solve the problem, simply add a dangling return after CleanExit. Because CleanExit will terminate the entire program, you won't have to worry about the significance of that value:

...
CleanExit(-1);
return -1;
...
1
  • 1
    On some compilers this will produce a warning about unreachable code
    – M.M
    Dec 27, 2014 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.