Main Content

Possible copy-paste error

A section of code is duplicated in other places with exactly one minor change

Since R2023a

Description

This defect occurs when a block of code is duplicated in multiple places with exactly one minor change.

The defect checker does not flag certain blocks as duplicates. For instance, this defect is not reported for blocks that are smaller than 6 lines. See also Duplicate Code Detection in Polyspace Bug Finder.

Risk

Sections of code that are exact duplicates apart from one minor change might indicate a programming error. You might have duplicated a section of code and later updated one of the duplicates but forgot to update the other.

Duplicated code also require unnecessary additional maintenance. See also Almost duplicated code and Duplicated code.

Fix

See if the one minor change between the two duplicated blocks is the result of a programming error. Fix the error.

In the long run, try to refactor the sections of code to reduce the duplication. For instance, identify parts of the code blocks that are duplicates of each other and refactor them into a dedicated function. You can then replace the duplicated code with calls to the dedicated function.

Examples

expand all

In this example, the functions getReadings() and isAllOk() contain a duplicate block of code with only one minor change. This one difference strongly indicates the possibility of a copy-paste error. It is possible that the logic for calculating the variables currentReading1 to currentReading8 is intended to be the same in the both functions. The logic was initially the same, but possibly updated later in one function but not in the other.

int pin1;
int pin2;
int pin3;
int pin4;
int pin5;
int pin6;
int pin7;
int pin8;


int currentReading1;
int currentReading2;
int currentReading3;
int currentReading4;
int currentReading5;
int currentReading6;
int currentReading7;
int currentReading8;

int allOk;

void getReadings() {
    currentReading1 = pin1; //Beginning of duplicate section #1
    currentReading2 = pin2 - pin1; 
    currentReading3 = pin2 + pin3;
    currentReading4 = pin4;
    currentReading5 = pin5;
    currentReading6 = pin6 - pin5 + pin2; //Minor change here
    currentReading7 = pin7;
    currentReading8 = pin8; //End of duplicate section #1
 
}

void isAllOk() {
    currentReading1 = pin1; //Beginning of duplicate section #2
    currentReading2 = pin2 - pin1;
    currentReading3 = pin2 + pin3;
    currentReading4 = pin4;
    currentReading5 = pin5;
    currentReading6 = pin6 - pin5 + pin1; //Minor change here
    currentReading7 = pin7;
    currentReading8 = pin8; //End of duplicate section #2
    
    if(currentReading1 > currentReading2
       && currentReading3 > currentReading4
       && currentReading5 > currentReading6
       && currentReading7 > currentReading8)
       allOk = 1;
}

The event list on the Result Details pane shows:

  • The beginning and end of each duplicate block of code.

  • The line where each block differs from the others.

Click on an event to navigate to the corresponding location in the source code.

Event list shows beginning and end of each duplicate section along with where each section differs from another.

In the Polyspace® user interface, you can see the duplicated code blocks side by side on the Source pane. See Navigate in Separate Window.

Correction – Fix Errors and Refactor Code to Avoid Duplication

If the issue flagged indicates a possible copy-paste error such as a forgotten update, fix the programming error. If possible, refactor your code to avoid the code duplication.

In this example, the function isAllOk() has been refactored so that it calls the function getReadings() to calculate variables currentReading1 to currentReading8. This refactoring eliminates chances of a copy-paste error.

int pin1;
int pin2;
int pin3;
int pin4;
int pin5;
int pin6;
int pin7;
int pin8;


int currentReading1;
int currentReading2;
int currentReading3;
int currentReading4;
int currentReading5;
int currentReading6;
int currentReading7;
int currentReading8;

int allOk;

void getReadings() {
    currentReading1 = pin1;
    currentReading2 = pin2 - pin1;
    currentReading3 = pin2 + pin3;
    currentReading4 = pin4;
    currentReading5 = pin5;
    currentReading6 = pin6 - pin5 + pin2;
    currentReading7 = pin7;
    currentReading8 = pin8;
 
}

void isAllOk() {
    getReadings();
    
    if(currentReading1 > currentReading2
       && currentReading3 > currentReading4
       && currentReading5 > currentReading6
       && currentReading7 > currentReading8)
       allOk = 1;
}

Result Information

Group: Programming
Language: C | C++
Default: Off
Command-Line Syntax: COPY_PASTE_ERROR
Impact: Low

Version History

Introduced in R2023a

expand all