Analyzer fixes -fshort-enums false positive in switch on enum.
Corrects a false positive warning in the analyzer when using -fshort-enums with switches on enum values.
With the -fshort-enums flag, the GCC analyzer could fail to recognize that a switch statement was operating on an enum value. This led to the analyzer incorrectly assuming that a default case could be taken, resulting in false “use of uninitialized value” warnings. This commit updates the switch_case_op::apply_constraints function to correctly handle the widening cast of an enum value when -fshort-enums is enabled, thus fixing the false positive.
In Details
The analyzer::switch_case_op::apply_constraints in ops.cc now correctly handles enum types when -fshort-enums is used. This flag causes enums to be represented using the smallest possible integer type. Previously, the analyzer did not properly account for this when evaluating switch statements on enums, leading it to believe a default case could be taken even when all enum values were explicitly handled. This commit ensures the widening cast of the enum is properly considered, fixing a false positive "use of uninitialized value" warning.
- analyzer
- A GCC component that statically analyzes code to find potential bugs, such as use-after-free, buffer overflows, and uninitialized values, without executing the code.
- -fshort-enums
- A GCC command-line option that instructs the compiler to use the smallest possible integer type to represent enumeration types, potentially reducing code size.
- use of uninitialized value
- A warning or error issued by a static analyzer or compiler when a variable is read before it has been assigned a defined value.