Skip to content

A7-1-1: false positive on non-const variables in class templates #18

Closed
@mbaluda

Description

@mbaluda

Affected rules

  • A7-1-1

Description

In the following code the member variable t_ is modified by the non-const member function Init. If the class is not a template no error is reported for similar code.

Example

template<typename T>
class A7_1_1b final {
 public:
  explicit A7_1_1b(std::int64_t i) noexcept { t_.Init(i); } // t_ is modified here by Init
 private:
  T t_; // <= A7-1-1: Non-constant variable t_ is used for an object and is not modified.
};

Example

#include <iostream>
#include <vector>

namespace test {

/// doc
template <class T>
class MyClass final {
 private:
  // CodeQL reports: A7-1-1:cpp/autosar/declaration-unmodified-object-missing-const-specifier
  // Non-constant variable data_ is used for an object and is not modified.
  // Marking this as "const" is incorrect, we can modify the data through the iterator returned
  // by the begin() function, adding const here does not compile.
  std::vector<T> data_;
};

}  // namespace test

/// main
int main(int, char**) noexcept {
  try {
    test::MyClass<int> c({1, 2, 3});
  } catch (const std::exception&) {
  }
}

Metadata

Metadata

Assignees

Labels

Difficulty-LowA false positive or false negative report which is expected to take <1 day effort to addressImpact-Mediumfalse positive/false negativeAn issue related to observed false positives or false negatives.user-reportIssue reported by an end user of CodeQL Coding Standards

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions