# V646. The 'else' keyword may be missing. Consider inspecting the program's logic.

The if operator is located in the same line as the closing parenthesis referring to the previous if. Perhaps, the key word 'else' is missing here, and the program works in a different way than expected.

Have a look at a simple example of incorrect code:

    if (A == 1) {
      Foo1(1);
    } if (A == 2) {
      Foo2(2);
    } else {
      Foo3(3);
    }

If the 'A' variable takes value 1, not only the 'Foo1' function will be called, but the 'Foo3' function as well. Note the program execution logic: maybe this is what the programmer actually expects it to do. Otherwise, the key word 'else' should be added.

This is the fixed code:

    if (A == 1) {
      Foo1(1);
    } else if (A == 2) {
      Foo2(2);
    } else {
      Foo3(3);
    }

The analyzer also considers the code correct when the 'then' part of the first 'if' operator contains the unconditional operator 'return' - because the program execution logic is not broken in this case, while it's just a bit incorrect code formatting. Here is an example of such a code:

    if (A == 1) {
      Foo1(1);
      return;
    } if (A == 2) {
      Foo2(2);
    } else {
      Foo3(3);
    }

If there is no error, the V646 warning can be avoided by moving the 'if' operator onto the next line. For example:

    if (A == 1) {
      Foo1(1);
    }
    if (A == 2) {
      Foo2(2);
    } else {
      Foo3(3);
    }

In the samples cited above, the error is clearly seen and seems improbable to be found in real applications. But if your code is quite complex, it becomes very easy not to notice the missing 'else' operator. Here is a sample of this error taken from a real application:

    if( 1 == (dst->nChannels) ) {
      ippiCopy_16s_C1MR((Ipp16s*)pDstCh, dstStep,
        (Ipp16s*)pDst, dst->widthStep, roi, pMask, roi.width);
    } if( 3 == (dst->nChannels) ) { //V646
      ippiCopy_16s_C3R((Ipp16s*)pDst-coi, dst->widthStep,
        (Ipp16s*)pTmp, dst->widthStep, roi);
      ippiCopy_16s_C1C3R((Ipp16s*)pDstCh, dstStep,
        (Ipp16s*)pTmp+coi, dst->widthStep, roi);
      ippiCopy_16s_C3MR((Ipp16s*)pTmp, dst->widthStep,
        (Ipp16s*)pDst-coi, dst->widthStep, roi, pMask, roi.width);
    } else {
      ippiCopy_16s_C4R((Ipp16s*)pDst-coi, dst->widthStep,
        (Ipp16s*)pTmp, dst->widthStep, roi);
      ippiCopy_16s_C1C4R((Ipp16s*)pDstCh, dstStep,
        (Ipp16s*)pTmp+coi, dst->widthStep, roi);
      ippiCopy_16s_C4MR((Ipp16s*)pTmp, dst->widthStep,
        (Ipp16s*)pDst-coi, dst->widthStep, roi, pMask, roi.width);
    }

This code is very hard to read and comprehend. But the analyzer always stays focused.

In this sample, the conditions '3 == (dst->nChannels)' and '1 == (dst->nChannels)' cannot be executed simultaneously, while the code formatting indicates that the key word 'else' is missing. This is what the correct code should look like:

    if( 1 == (dst->nChannels) ) {
      ....
    } else if( 3 == (dst->nChannels) ) {
      ....
    } else {
      ....
    }

This diagnostic is classified as:

CWE-670

You can look at examples of errors detected by the V646 diagnostic.