Thursday, April 14, 2016

How to fix the “deadcode” type coverity warnings in C code

    Coverity is a static analysis tool used to analyze and detect the defects in our coding. Coverity tool finds the logical errors also present in the code.


Deadcode:
                It is a type of error reported by the coverity tool. A part of code is marked as “dead” if that part of code is never getting executed. Please find below the illustration for the same.

Coverity “Deadcode” Example:

void SetRowStatus (int RowStatus)
{
                if ((RowStatus < 1) || (RowStatus > 6))
                {
                                return;
                }
                switch RowStatus:
                {
                                case 1:
                                                ……
                                                ……
                                                break;
                                case 2:
                                                ……
                                                ……
                                                break;
                                case 3:
                                                ……
                                                ……
                                                break;
                                case 4:
                                                ……
                                                ……
                                                break;
                                case 5:
                                                ……
                                                ……
                                                break;
                                case 6:
                                                ……
                                                ……
                                                break;
                                default:
                                                ……                         ----> DEADCODE
                                                return;

                }
                return;
}

    In the above code snippet, the "RowStatus" variable takes on the value of 1 to 6. If the "RowStatus" value does not lie in the range of 1 to 6, the function would return in the “if” check itself. So, it is deterministic that the “default” case would never get executed in the switch block of code. So, the part of code inside the “default” case will be marked as “deadcode” by the coverity tool.

Resolving DeadCode Error:
                We might think that, removing the “default” case from the switch block of code would resolve the issue. But it will lead to the compiler warning (default case is missing). So, the correct approach is given below.

void SetRowStatus (int RowStatus)
{
                if ((RowStatus < 1) || (RowStatus > 6))
                {
                                return;
                }
                switch RowStatus:
                {
                                case 1:
                                                ……
                                                ……
                                                break;
                                case 2:
                                                ……
                                                ……
                                                break;
                                case 3:
                                                ……
                                                ……
                                                break;
                                case 4:
                                                ……
                                                ……
                                                break;
                                case 5:
                                                ……
                                                ……
                                                break;
                                default: /* case 6*/
                                                ……
                                                ……
                                                break;
                               
                }
                return;
}


    Now, we are making the “case 6” as the “default” case, so that the “deadcode” coverity error as well as the compiler warning will get resolved.


No comments:

Post a Comment