转至元数据结尾

转至元数据起始
C++标准[dcl.type.cv]第4段[ISO/IEC 14882-2014]规定如下:
除了可以修改声明的任何类成员之外,任何在对象生存期内修改对象的尝试都会导致未定义的行为。mutableconst
同样,第6款规定如下:
构成对具有 volatile 限定类型的对象的访问的内容是实现定义的。如果尝试通过使用具有非易失性限定类型的 glvalue 来引用使用 volatile 限定类型定义的对象,则程序行为是未定义的。
不要放弃某个限定条件来尝试修改生成的对象。限定符意味着 API 设计者不打算修改该对象,尽管它可能是可修改的。不要抛弃资格;限定符意味着 API 设计器打算以编译器未知的方式访问对象,并且对易失性对象的任何访问都会导致未定义的行为。constconstvolatilevolatile

不合规代码示例

在这个不符合的代码示例中,函数 () 被传递给一个 const int &,然后将其转换为 int & 并进行修改。由于引用的值之前已声明为 const,因此赋值操作会导致未定义的行为。g

void g(const int &ci) {
int &ir = const_cast<int &>(ci);
ir = 42;
}

void f() {
const int i = 4;
g(i);
}

合规解决方案

在此兼容解决方案中,该函数被传递一个 ,并且调用方需要传递一个可以修改的函数。g()int &int

void g(int &i) {
i = 42;
}

void f() {
int i = 4;
g(i);
}

不合规代码示例

在此不符合的代码示例中,调用了一个 -qualified 方法,该方法尝试通过删除 的 -限定符来缓存结果。因为被宣布,突变导致未定义的行为。constconstthissconstcachedValue

#include

class S {
int cachedValue;

int compute_value() const; // expensive
public:
S() : cachedValue(0) {}

// …
int get_value() const {
if (!cachedValue) {
const_cast(this)->cachedValue = compute_value();
}
return cachedValue;
}
};

void f() {
const S s;
std::cout << s.get_value() << std::endl;
}

合规解决方案

此兼容解决方案在声明 时使用关键字,该关键字允许在上下文中发生突变,而不会触发未定义的行为。mutablecachedValuecachedValueconst

#include

class S {
mutable int cachedValue;

int compute_value() const; // expensive
public:
S() : cachedValue(0) {}

// …
int get_value() const {
if (!cachedValue) {
cachedValue = compute_value();
}
return cachedValue;
}
};

void f() {
const S s;
std::cout << s.get_value() << std::endl;
}

Noncompliant Code Example

In this noncompliant code example, the volatile value has the qualifier cast away, and an attempt is made to read the value within , resulting in undefined behavior.svolatileg()

#include

struct S {
int i;

S(int i) : i(i) {}
};

void g(S &s) {
std::cout << s.i << std::endl;
}

void f() {
volatile S s(12);
g(const_cast(s));
}

Compliant Solution

This compliant solution assumes that the volatility of is required, so is modified to accept a sg()volatile S &.

#include

struct S {
int i;

S(int i) : i(i) {}
};

void g(volatile S &s) {
std::cout << s.i << std::endl;
}

void f() {
volatile S s(12);
g(s);
}

Exceptions

EXP55-CPP-EX1: An exception to this rule is allowed when it is necessary to cast away when invoking a legacy API that does not accept a argument, provided the function does not attempt to modify the referenced variable. However, it is always preferable to modify the API to be -correct when possible. For example, the following code casts away the qualification of in the call to the function.constconstconstconstINVFNAMEaudit_log()

// Legacy function defined elsewhere - cannot be modified; does not attempt to
// modify the contents of the passed parameter.
void audit_log(char errstr);

void f() {
const char INVFNAME[] = “Invalid file name.”;
audit_log(const_cast<char
>(INVFNAME));
}

Risk Assessment

如果对象被声明为常量,则它可能在运行时驻留在写保护内存中。尝试修改此类对象可能会导致程序异常终止或拒绝服务攻击。如果某个对象被声明为易失性对象,则编译器不能对该对象的访问做出任何假设。丢弃对象的易失性可能会导致对对象的读取或写入被重新排序或完全省略,从而导致异常程序执行。

Rule Severity Likelihood Remediation Cost Priority Level
EXP55-CPP 中等 或然 中等 P8 二层

自动检测

Tool Version Checker Description
阿斯特雷 20.10 **指针限定符-cast-constpointer-qualifier-cast-volatile

| 部分检查 | | 阿克西维恩包豪斯套房 | 7.2.0 | 证书C++-EXP55** |

| | 螺旋 QAC | 2021.2 | C++3066, C++4671 |

| | 克洛乔夫 | 2021.4 | 米斯拉。投。常量 |

| | LDRA 工具套件 | 9.7.1
| 南纬203、242南、344南
| 全面实施 | | Parasoft C/C++test | 2021.2 |
CERT_CPP-EXP55-a | 强制转换不得从指针或引用类型中删除任何”const”或”volatile”限定 | | 多空间错误查找器 | R2021b | 证书C++: EXP55-CPP | 检查删除指针 cv 限定的强制转换(部分覆盖规则) | | PRQA QA-C++ | 4.4 | 3066, 4671** |

| | 规则检查器 | 20.10 | **指针限定符-cast-constpointer-qualifier-cast-volatile

** | 部分检查 | | SonarQube C/C++ 插件 | 4.10 | S858型 |

|

相关漏洞

在 CERT 网站上搜索因违反此规则而导致的漏洞。

相关准则

SEI CERT C 编码标准 EXP32-C.不要通过非易失性参考电压源
EXP40-C访问易失性对象。不修改常量对象

书目

[ISO/IEC 14882-2014
]
子句 7.1.6.1,”cv 限定符“
[萨特 2004
]
项目94,”避免抛弃const”