【C/C++】error: jump to case label


problem

The following is not allowed:
switch (a)
{
    case 1:
        int a = 6;
        //stuff
        break;

    case 2:
        //stuff
        break;
}

The following is allowed:
switch (a)
{
    case 1:
        {
            int a = 6;
            //stuff
        }
        break;

    case 2:
        //stuff
        break;
}



Reason

The ‘jump’ in the error message is the computed goto effected by the switch statement.
When x = 1, the switch acts like this:
goto case_label_1;
// …
unsigned int y = ++ x;
// …
case_label_1:
// here y is uninitialized

The problem is that the initialization of y is skipped when x == 1.
When the "case 1:" label is reached, stack space has been allocated for
y but its value has not been initialized. This is not allowed.

The general way round this situation is to make the scope of y smaller
by adding braces:
switch (x)
{
default:
unsigned z; // this is OK since z is uninitialized
{
unsigned y = x + 1;
// …
}
case 1:
z = 2;
// …
// cannot refer to y here so no problem with y’s initialization
}


An error example from L4

user/serv/sigma0/sigma0.cc
static void init_mempool_from_kip (void)
{
    …

        switch (L4_MemoryDescType (md))
        {
        case L4_UndefinedMemoryType:
          break;
        case L4_ConventionalMemoryType:
          conv_memory_pool.insert (low, high, L4_anythread);
          break;
        case L4_ReservedMemoryType:
          alloc_pool.insert (low, high, kernel_id);
          break;
        case L4_DedicatedMemoryType:
          memory_pool.insert (low, high, L4_anythread);
          break;
        case L4_SharedMemoryType:
          alloc_pool.insert (low, high, L4_anythread);
          break;
        default:
          dprintf (0, "s0: Unknown memory type (0x%x)n",
               (int) L4_MemoryDescType (md));
          break;

    …
}

# make
sigma0.cc: In function ‘void init_mempool_from_kip()’:
sigma0.cc:1060: error: jump to case label
sigma0.cc:1058: error:   crosses initialization of ‘L4_ThreadId_t <anonymous>’
sigma0.cc:1063: error: jump to case label
sigma0.cc:1058: error:   crosses initialization of ‘L4_ThreadId_t <anonymous>’
sigma0.cc:1068: error: jump to case label
sigma0.cc:1058: error:   crosses initialization of ‘L4_ThreadId_t <anonymous>’
sigma0.cc:1073: error: jump to case label
sigma0.cc:1058: error:   crosses initialization of ‘L4_ThreadId_t <anonymous>’
make: * [sigma0.o] Error 1


L4_anythread is defined as:
typedef union {
    L4_Word_t           raw;
    L4_GthreadId_t      global;
    L4_LthreadId_t      local;
} L4_ThreadId_t;

#define L4_nilthread      ((L4_ThreadId_t) { raw : 0UL})
#define L4_anythread      ((L4_ThreadId_t) { raw : ~0UL})


The following will be right
static void init_mempool_from_kip (void)
{
    …

        switch (L4_MemoryDescType (md))
        {
      case L4_UndefinedMemoryType:
          break;
        case L4_ConventionalMemoryType:
        {
             conv_memory_pool.insert (low, high, L4_anythread);
         break;
        }
        case L4_ReservedMemoryType:
        alloc_pool.insert (low, high, kernel_id);
        break;
        case L4_DedicatedMemoryType:
            {
            memory_pool.insert (low, high, L4_anythread);
      break;
            }
        case L4_SharedMemoryType:
            {
              alloc_pool.insert (low, high, L4_anythread);
      break;
            }
        default:
          dprintf (0, "s0: Unknown memory type (0x%x)n",
               (int) L4_MemoryDescType (md));
          break;
        }

    …

}








end