Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enforce the usage of return values in C

Tags:

c

gcc

I'm searching for a compiler flag for gcc and if possible for clang and the Microsoft compilers as well, that triggers a warning (error with -Werror) if a non-void function is called without using the return value like this:

int test() {
    return 4;
}

int main(void) {
    test(); //should trigger a warning
    int number = test(); //shouldn't trigger the warning
    return 0;
}

If there is no such compiler flag, maybe some way to tell the clang static analyzer to complain about it.

EDIT: To clarify my original question: I actually meant using the return value, not only assigning it.

like image 744
FSMaxB Avatar asked Sep 04 '15 06:09

FSMaxB


People also ask

How do you use a returned value from a function?

To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.

How do you use return in C?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function. For more information, see Return type.

Why is it important to have return values?

Generally, a return value is used where the function is an intermediate step in a calculation of some kind. You want to get to a final result, which involves some values that need to be calculated by a function.

What happens if you dont use return 0 in C?

Short Answer: Nothing. Better Answer: return 0 it's used in main like a signal for know the exit of program was a success when return 0 executes. Best Answer: Still nothing because compilers already "put" return 0 in the the end of your code if you not explicit.


2 Answers

I never used it myself (do you really need it?), you can try

  • defining the function with warn_unused_result attribute
  • enabling -Wunused-result flag with gcc.

This will tell you about any unused value from the function return.


In case, any doubts, SEE IT LIVE or SEE IT LIVE AGAIN Thanks to M.M for the link in the comment

Or:

#include <stdio.h>

extern int func1(void) __attribute__((warn_unused_result));
extern int func2(void);

int main(void)
{
    func1();
    int rc1 = func1();
    int rc2 = func1();
    func2();
    printf("%d\n", rc1);
    return 0;
}

Compilation (GCC 5.1.0 on Mac OS X 10.10.5):

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -c warn.c
warn.c: In function ‘main’:
warn.c:10:9: error: unused variable ‘rc2’ [-Werror=unused-variable]
     int rc2 = func1();
         ^
warn.c:8:5: error: ignoring return value of ‘func1’, declared with attribute warn_unused_result [-Werror=unused-result]
     func1();
     ^
cc1: all warnings being treated as errors
$
like image 123
Sourav Ghosh Avatar answered Oct 01 '22 08:10

Sourav Ghosh


Some static code analyzers like splint can check for these kind of things:

$ splint check.c
Splint 3.1.2 --- 03 May 2009

check.c: (in function main)
check.c:6:5: Return value (type int) ignored: test()
  Result returned by function call is not used. If this is intended, can cast
  result to (void) to eliminate message. (Use -retvalint to inhibit warning)

Unlike @Sourav's answer, this does not require a specific __attribute__ annotation on the target function, but on the other hand possibly emits many warnings. Its usually possible to suppress the warnings for specific functions or function calls by using annotations (like /*@alt void@*/).

like image 24
Andreas Fester Avatar answered Oct 01 '22 08:10

Andreas Fester