Skip to main content

Branch coverage and unreachable branches

Task 1 

For each example from the section "Statement coverage and unreachable code" (Task 1) of this book, create a Control Flow Graph and identify  branches that are unreachable.

  1. Return Statement Before Print Statement
#include <stdio.h>
int main() 
{
    printf("text_1"); 
    return 0; 
    printf("text_2");
}

image.pngЗнімок екрана 2025-05-13 133540.png

Unreachable:

  • printf("text_2");
    (It comes after the return, so it will never execute.)

2. Infinite Loop Before Statements

#include <stdio.h>
int main() 
{
  int x = 5;
  for (;;) 
  {
    if (x == 5)
{
  break;
  printf("Hello World");
}
}
  return 0;
}

image.pngimage.png

Unreachable:

  • printf("Hello World");
    (It is placed after break, so it will never be executed.)

3. Continue Before Print

#include <stdio.h>
int main() 
{
  for (int i = 0; i < 10; i++...) {
  continue;
  printf("Hello World");
}
return 0;
}

image.pngЗнімок екрана 2025-05-13 134423.png


Unreachable:

  • printf("Hello World");
    (zawsze pomijane przez continue)

4. False Condition in if Statement

#include <stdio.h>
int main()
{
  double X = 1.55;
if (X > 5) 
  {
  X++;
}
return 0;
}

image.pngЗнімок екрана 2025-05-13 134436.png


Efectively Unreachable at(dla Runtimetej (butkonkretnej structurallywartości reachable)X):

    • X++;
      This line is not executed at runtime for X = 1.55, since the condition(ponieważ X > 5 isjest false.fałszywe, However,ciało if nie jest wykonywane itdla couldtej bewartości; executedjednak ifkod thesam valuew ofsobie Xnie werejest different,strukturalnie sonieosiągalny it’s notjest markedtylko aspomijany unreachablew intym the graph syntax.przypadku)

 Task 2 

For each example from the section "Statement coverage and unreachable code" (Task 2) of this book, create a Control Flow Graph and identify  branches that are unreachable.

1. Return statement before if statement.

int func(int a)
{
   int b = a*2;
   return b;

   // Unreachable code
   if (b < 10)
   {
        b += 10;
   }
   return b;
}

Unreachable:

Code below return statement is unreachable.

1.png2. Wrong vector size

vector<string> vec;
while (true)
{
   vec.push_back("abc");

   if (vec.size() > 25)
   {
       return 0;
   }
}

// Unreachable code
vec.pop_back();

Unreachable:

The infinite loop in this sample fills the 'vec' container with the string "abc". As the container's size exceeds 25, the return operator will be called to immediately terminate the function and bring control back to the return point. Due to this, the last item of the 'vec' container will never be removed.

 

2.png3. Always false condition

tStatus = TGetFirstLayerItem (pContext, DBX_LAYER_MULTI, pLayerItem);
...
if (DBX_LAYER_MULTI && tStatus == DBX_OK)
{
    // Writing styles
}

Unreachable: In this sample, the list of project styles will never be written because the condition (DBX_LAYER_MULTI && tStatus == DBX_OK) is always false. It contains the DBX_LAYER_MULTI constant which equals zero.

3.png4.  Unsigned variable

typedef unsigned short wint_t;

void lexungetc(wint_t c) {
  if (c < 0)
    return;
  ....
}

Unreachable: 

The condition 'c<0' here is always false because 'c' is unsigned.

4.png