INCONSISTENT.LABEL

Inconsistent case labels

The INCONSISTENT.LABEL checker finds situations in which more than one enum type is used as a switch expression or as a label in a switch statement.

Vulnerability and risk

Using labels with different enum types in a switch statement can cause problems because enum members with the same value can have different meanings. The design intent fails, and unexpected results can occur.

Vulnerable code example

Copy
  typedef enum Q1{Q1Send, Q1Recv} Q1;
  typedef enum Q2{Q2None, Q2Send, Q2Recv} Q2;
  
  // Inconsistency between switch variable and case labels
  void foo1(Q1 q) {
    switch (q) {
      case Q2Send: f(); break;
      case Q2Recv: g(); break;
    }
 }
 
 //Inconsistency between case labels
 void foo2(Q1 q) {
   switch (q) {
     case Q1Send: f(); break;
     case Q2Recv: g(); break;
   }
 }

Klocwork flags line 8 for foo1() to indicate that the type of variable used in the switch is different from the types of the case labels. Klocwork flags line 16 for foo2() to indicate that the case labels use enumerators from two different enum types.