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;
                }

Tuesday, April 5, 2016

Leonardo & the SubString - HackrRank Challenge


Leonardo & the SubString:

  • In this post we will solve the programming challenge of "Leonardo & the substring" matching posted in HackrRank.

Problem Description:

Leonardo loves puzzles involving strings, but he's just found a problem that has him stumped! Help him solve the following challenge:
Given a binary string, , composed of only 's and 's, find and print the total number of substrings of  which do not contain a  or .
Input Format
The first line contains an integer, (the number of test cases). 
The  subsequent lines of test cases each contain a string, , composed only of 's and 's.
Constraints
Output Format
For each test case, print the total number of substrings of  having no consecutive zeroes or ones (i.e.: not containing  or ).