Main Content

Number of goto statements exceeds threshold

The number of goto statements in a function is greater than the defined threshold

Since R2021a

Description

This defect is raised on a function when the number of goto statements in the function is greater than the define threshold. For details about how Polyspace calculates the number of goto statements, see Number of Goto Statements

Polyspace® uses the default threshold zero unless you specify a threshold. To specify a selection file where you can set the threshold, use the option Set checkers by file (-checkers-selection-file) or Checkers activation file (-checkers-activation-file).

When you import comments from previous analyses by using polyspace-comments-import, Polyspace copies any review information on the code metric Number of Goto Statements in the previous result to this checker in the current result. If the current result contains the same code metric, the review information is copied to the code metric as well.

Risk

Violation of this checker might indicate that:

  • The module has an overly complicated flow of execution.

  • The module might contain unexpected or unplanned development.

With multiple goto statements, it is difficult to determine the exact order of code execution in the module. This confusion might lead to bugs or an incorrect result. The complex data flow of such code makes the module difficult to maintain and debug.

Fix

To fix this check, either change the checker threshold or refactor your code. You can replace most uses of the goto statement by more straightforward control structures. In instances where a goto statement is necessary, document it and justify the checker by using an annotation in the code. You might want to change the threshold to avoid triggering this check when checking legacy codebases.

A best practice is to check the complexity of a module early in development to avoid costly post-development refactoring.

Examples

expand all

#include<stdbool.h>
bool* getCondition();
void foo(){//Noncompliant
	bool* cond;
	cond = getCondition();
	while(*(cond+0)){
		//...
		while(*(cond+1)){
			//...
			while(*(cond+2)){
				//...
				if(*(cond+3)){
				goto HARDBREAK;
				}
			}
		}
	}
HARDBREAK:
		;
		//....
		return;
}

In this example, a goto statement is used for conditionally breaking the flow of execution out of a deeply nested control structure. Polyspace flags the function because the number of goto statements in the function greater than the defined threshold, which is zero by default.

Correction — Refactor the Code or Change the Threshold

One possible correction is to refactor the code so that there are no goto statements in the code. For instance, you might want to use a return statement to get out of a deeply nested structure.

#include<stdbool.h>
bool* getCondition();
void foo(){//Compliant
	bool* cond;
	cond = getCondition();
	while(*(cond+0)){
		//...
		while(*(cond+1)){
			//...
			while(*(cond+2)){
				//...
				if(*(cond+3)){
				return;
				}
			}
		}
	}

}

Alternatively, you might consider this instance of goto as acceptable. In that case, change the threshold by modifying the checkers selection XML file to resolve the check.

Check Information

Group: Software Complexity
Language: C | C++
Acronym: SC13
Default Threshold: 0

Version History

Introduced in R2021a