Why does -fsanitize=enum not work in C?

I am investigating enabling -fsanitize=enum for kernel builds but realized that this sanitizer seems to do nothing?

Here’s a godbolt linking show this: Compiler Explorer

Is there a reason this sanitizer only works for C++ and can it be expanded to work for C? I’d be willing to tackle this if it seems reasonable.

Thanks
Justin

As far as I know, the language rule in question doesn’t exist in C, so there’s nothing to diagnose.

I’m new to the codebase, could you elaborate on what you mean by “the language rule doesn’t exist”? Do you mean that enums are treated fundamentally differently between C and C++? I know C++ has enum classes but am specifically referring to basic enums here.

The C++ rule is [dcl.enum]p8: " For an enumeration whose underlying type is fixed, the values of the enumeration are the values of the underlying type. Otherwise, the values of the enumeration are the values representable by a hypothetical integer type with minimal width M such that all enumerators can be represented."

The equivalent rule in C is in 6.7.2.2: “After possible lvalue conversion a value of the enumerated type behaves the same as the value with the underlying type, in particular with all aspects of promotion, conversion, and arithmetic.”

So the given example is undefined behavior in C++, but not in C.

Regardless of whether it’s UB or not in C, it would still be nice to have a sanitizer be able to catch unintentional enum values at runtime.

-fsanitize=enum only checks loads, IIUC. This misses truncations and other conversations.