Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
langcpp
#include <algorithm>
#include <vector>

class A {
  int m;
  
public:
  A() : m(0) {}
  explicit A(int m) : m(m) {}
  
  A(const A &other) : m(other.m) {}
  A(A &&other) : m(other.m) { other.m = 0; }
  
  A& operator=(const A &other) {
    if (&other != this) {
      m = other.m;
    }
    return *this;
  }
 
  A& operator=(A &&other) {
    m = other.m;
    other.m = 0;
    return *this;
  }
  
  int get_m() const { return m; }
};

void f() {
  std::vector<A> v{10};
  A obj(12);
  std::fill(v.begin(), v.end(), obj);
}

Exceptions

OOP58-CPP-EX0: Reference counting, and implementations such as std::shared_ptr<> constitute an exception to this rule. Any copy or assignment operation of a reference-counted object requires the reference count to be incremented. The semantics of reference counting are well-understood, and it can be argued that the reference count is not a salient part of the shared_pointer object.

Risk Assessment

Copy operations that mutate the source operand or global state can lead to unexpected program behavior. Using such a type in a Standard Template Library container or algorithm can also lead to undefined behavior.

...

Tool

Version

Checker

Description

CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

LANG.FUNCS.COPINC

Copy Operation Parameter Is Not const

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C++4075
Klocwork
Include Page
Klocwork_V
Klocwork_V

CERT.OOP.COPY_MUTATES 


Parasoft C/C++test

Include Page
Parasoft_V
Parasoft_V

CERT_CPP-OOP58-a

Copy operations must not mutate the source object
Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT  PRQA QA- C++ 4.1: OOP58-CPPChecks for copy operation modifying source operand (rule partially covered)4075, 4076 

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...