Gcc warning for different return types

I wrote a library in C that will now be used in the integrated processor board. I need to reduce the printing of memory folders, so that there is a change in the return type of some functions from int to char (used only for the error flag).

Changing the return type, but not changing the return variable, does not raise a warning. Is there any way to install this in gcc as I want to make sure that I have caught all instances.

char processSomething (SomeType *something) { int result = 0; ... do stuff ... return result; /* no warning */ } 

thanks

June 19: I added -Wconversion, which highlighted some interesting things. Two questions. One of my functions is taking a two-character hexadecimal string and converting to decimal using

char decimal;

 decimal = hexstring [0] - '0' << 4 + hexstring [1] - '0'; // for 0 to 9 

he complains about converting from int to char and from what I can compile, these are the '-', '+' and '<<operators all seem to be implicitly converted to int. Is this correct or am I interpreting the warning incorrectly. Or is there a better way to do this?

Also, when using strtol in int (complains about 'long int' for 'int'), but I can guarantee that the value will not exceed even a short int in size, is this safe for typecast? As in the case of truncating extra (and, hopefully, zero bytes) long? Or does it depend on the essence?

thanks

+6
source share
2 answers

In this particular case - Wconversion should give you the warning you want in my simple case ( watch live ):

 char func() { int x = 10 ; return x ; } int main() {} 

I get the following warning:

warning: converting to 'char' from 'int' may change its value [-Wconversion]

+7
source

This is a really bad idea: char is a certain size for most embedded systems (usually 1 byte, so it's actually an unsigned char ), and if you somehow set up int result = 256 , it will overflow and return 0. Your error turned into a success. Crashy crashy (hopefully). Worse, maybe kill someone with a built-in device.

I know that you are trying to fix it, but even just returning magic numbers are dangerous.

Instead, declare an enum type that is an error. This will give you type safety (to some extent) and automatically create the correct return size for your functions.

 typedef enum status { STATUS_OK = 0, STATUS_ERR1, STATUS_ERR2, // ... etc ... } status_t; status_t processSomething (SomeType *something) { status_t result = STATUS_OK; ... do stuff ... return result; } 

This is much safer, and the compiler will only allocate 1 byte for your returns until you have too many suitable ones, and only then it will make it bigger. Adapted from current standard C (C99): http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

6.7.2.2 Enumeration qualifiers [...] Constraints An expression specifying the value of an enumeration constant must be an integer constant expression that has a value that can be represented as int. [...] Each enumerated type must be compatible with char, an integer type with a signed or unsigned integer type. The choice of type is defined by the implementation, but should be able to represent the values ​​of all members of the enumeration.

+2
source

Source: https://habr.com/ru/post/989304/


All Articles