EN JP CN

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

1  typedef enum Q1{Q1Send, Q1Recv} Q1;
2  typedef enum Q2{Q2None, Q2Send, Q2Recv} Q2;
3  
4  // Inconsistency between switch variable and case labels
5  void foo1(Q1 q) {
6    switch (q) {
7      case Q2Send: f(); break;
8      case Q2Recv: g(); break;
9    }
10 }
11 
12 //Inconsistency between case labels
13 void foo2(Q1 q) {
14   switch (q) {
15     case Q1Send: f(); break;
16     case Q2Recv: g(); break;
17   }
18 }

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.