zzxyz
zzxyz

Reputation: 2981

enabling "differing levels of indirection" warning/error in gcc

We have a handle type declared like:

typedef void *config_h;

We have a function declared like:

void func(config_h hConfig);

I called it like this:

config_h hConfig;
func(&hConfig);

Not even a warning. Things I cannot change about this project: It is C++11, and compiled with -fpermissive. The config_h typedef is done in a file that is also compiled by C compilers, btw..

I looked here: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

Looked like -Wstrict-aliasing might be the ticket, but it was not. I get loads of warnings about various pointer conversions, but not this one.

The question is "What is the narrowest warning (or preferably error) I can enable to prevent this problem?"

Bonus question: If stuff like this drives me nuts, is switching to clang likely to pay dividends?

Upvotes: 1

Views: 104

Answers (1)

zzxyz
zzxyz

Reputation: 2981

As was pointed out to me by several folks in the comments (thanks all), even differing levels of indirection aren't considered when it comes to void*. I verified this in other compilers. So the answer to my actual question, best I can tell, is that there is no warning or error that can be enabled, regardless of compiler.

That said, if the void* actually points to a concrete type, it is relatively painless, even in very large solutions to clean this up. I went with forward-declaring the types:

struct config_o;
typedef config_o* config_h;
//removed typedef void* config_h;

I was able to clean up about 40 handle types in 200,000 lines of code in significantly less than 8 hours. Found (and fixed) several serious bugs while I was at it, which consumed the majority of the time.

Upvotes: 1

Related Questions