https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81598

Julien Blanc <julien.blanc at sprinte dot eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |julien.blanc at sprinte dot eu

--- Comment #9 from Julien Blanc <julien.blanc at sprinte dot eu> ---
I recently ran into this while trying to use -fsanitize=enum for the codebase.
The situation is worse for C++ enum class : since you can only assign them with
static_cast, no load check can be done (clang also fails to detect such cases).

Here’s a sample code that should trigger but does not :

#include <iostream>

enum class Foo
{
        foo1 = 0,
        foo2 = 1
};
std::ostream& operator<<(std::ostream& o, Foo foo)
{
        switch(foo)
        {
                case Foo::foo1:
                        return o << "foo1";
                case Foo::foo2:
                        return o << "foo2";
        }
        return o << "unknown";
}
int main()
{
        Foo foo = static_cast<Foo>(3);
        std::cout << foo << std::endl;
        return 0;
}


$ g++ -fsanitize=enum -fsanitize=undefined -fno-sanitize-recover enum.cpp 
$ ./a.out 
unknown
$

Note that clang++ is no better :

$ clang++-7 -fsanitize=enum -fsanitize=undefined -fno-sanitize-recover=all
enum.cpp 
$ ./a.out 
unknown
$

But i’d expect both checkers to detect such misuse. While it could be valid
code, there’s a high chance that its a bug. Ideally an attribute could be used
if it is expected behaviour.

Reply via email to