- 由 Aaron Ballman创建, 最终由 Jon O’Donnell 修改于 十二月 29, 2021
转至元数据起始
C++标准[basic.types]第9段[ISO/IEC 14882-2014]规定如下:
类型对象的对象表示形式是类型对象占用的 N 个对象的序列,其中 N 等于 。对象的值表示形式是保存类型 值 的一组位。Tunsigned charTsizeof(T)T
窄字符类型 (, 和 )(以及特定平台上的其他一些整数类型)具有仅由对象值表示中的位组成的对象表示形式。对于此类类型,访问值表示形式的任何位都是明确定义的行为。这种形式的对象表示允许程序员仅基于对象的位表示来访问和修改对象,例如通过调用其对象表示形式。charsigned charunsigned charstd::memcmp()
其他类型(如类)可能没有仅由对象值表示中的位组成的对象表示形式。例如,类可能具有位字段数据成员、在数据成员之间插入的填充、支持虚拟方法调度的 vtable 或声明具有不同访问权限的数据成员。对于此类类型,访问不属于对象值表示形式的对象表示形式的位可能会导致未定义的行为,具体取决于这些位的访问方式。
不要访问不属于对象值表示形式的对象表示形式的位。即使以明确定义的方式(例如通过对象数组)访问位,这些位表示的值也是未指定或实现定义的,并且依赖于任何特定值都可能导致异常程序执行。unsigned char
不合规代码示例
在此不符合的代码示例中,在比较两个类型为 的对象时,将访问完整的对象表示形式。根据C++标准[类]第13段[ISO/IEC 14882-2014],类可以填充数据,以确保它们在内存中正确对齐。填充的内容和添加的填充量是实现定义的。在比较类的对象表示形式而不是值表示形式时,这可能会导致不正确的结果,因为填充可能会为每个对象实例假定不同的未指定值。S
| #include struct S { unsigned char buffType; int size; }; void f(const S &s1, const S &s2) { if (!std::memcmp(&s1, &s2, sizeof(S))) { // … } } |
|---|
合规解决方案
在此兼容解决方案中,重载以执行对象的值表示形式的比较。Soperator==()
| struct S { unsigned char buffType; int size; friend bool operator==(const S &lhs, const S &rhs) { return lhs.buffType == rhs.buffType && lhs.size == rhs.size; } }; void f(const S &s1, const S &s2) { if (s1 == s2) { // … } } |
|---|
不合规代码示例
在此不符合的代码示例中,用于清除对象的内部状态。由于存在虚拟函数,实现可能会在对象实例中存储 vtable,并且该 vtable 随后被调用 覆盖,从而导致在需要虚拟方法调度时出现未定义的行为。std::memset()std::memset()
| #include struct S { int i, j, k; // … virtual void f(); }; void f() { S s = new S; // … std::*memset(s, 0, sizeof(S)); // … s->f(); // undefined behavior } |
|---|
合规解决方案
在此兼容解决方案中,将显式清除 的数据成员,而不是调用Sstd::memset().
| struct S { int i, j, k; // … virtual void f(); void clear() { i = j = k = 0; } }; void f() { S *s = new S; // … s->clear(); // … s->f(); // ok } |
|---|
Exceptions
EXP62-CPP-EX1: It is permissible to access the bits of an object representation when that access is otherwise unobservable in well-defined code. Specifically, reading bits that are not part of the value representation is permissible when there is no reliance or assumptions placed on their values, and writing bits that are not part of the value representation is only permissible when those bits are padding bits. This exception does not permit writing to bits that are part of the object representation aside from padding bits, such as overwriting a vtable pointer.
For instance, it is acceptable to call on an object containing a bit-field, as in the following example, because the read and write of the padding bits cannot be observed. std::memcpy()
| #include struct S { int i : 10; int j; }; void f(const S &s1) { S s2; std::memcpy(&s2, &s1, sizeof(S)); } |
|---|
Code that complies with this exception must still comply with OOP57-CPP. Prefer special member functions and overloaded operators to C Standard Library functions.
Risk Assessment
访问不属于对象值表示形式的对象表示形式的位的效果可以从实现定义的行为(如假定具有不同访问控制的字段的布局)到代码执行漏洞(如覆盖 vtable 指针)。
| Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
| EXP62-CPP | 高 | 或然 | 高 | 小六 | 二层 |
自动检测
| Tool | Version | Checker | Description |
|---|---|---|---|
| 阿斯特雷 | 20.10 | invalid_pointer_dereference uninitialized_variable_use |
|
| CodeSonar | 6.2p0 | BADFUNC.MEMCMP
BADFUNC.内存组 | memcmp 的使用
使用摺带 |
| 螺旋 QAC | 2021.2 | C++4726, C++4727, C++4728, C++4729, C++4731, C++4732, C++4733, C++4734 |
|
| LDRA 工具套件 | 9.7.1
| 618 秒
| 部分实施 |
| Parasoft C/C++test | 2021.2 | CERT_CPP-EXP62-a** | 不要将可能包含填充位的类的对象与 C 标准库函数进行比较 |
| PVS-工作室 | 7.17 | V598
、 V780 |
|
相关漏洞
相关准则
| SEI 认证C++编码标准 | OOP57-CPP.优先选择特殊成员函数和重载运算符,而不是 C 标准库函数 |
|---|---|
书目
| [ISO/IEC 14882-2014 ] |
子句 3.9、”类型” 子句 3.10、”左值和右值” 第 9 条、”类” |
|---|---|
我的其他评论现在已经解决,但我发现了另外两个:某些类型(例如,整数类型,如 int 和 wchar_t)具有仅由对象值表示中的位组成的对象表示形式。
大卫·斯沃博达 发表:
- 对于 C,这只适用于 …所有其他整数类型都允许具有填充位。C++也是如此吗?unsigned char
- 提供一个链接来解释什么是vtable是很好的,但链接应该是比维基百科更权威的东西。理想情况下,在我们的参考书目中引用。
- 十月 06, 2016
> 对于C,这只适用于…所有其他整数类型都允许具有填充位。C++也是如此吗?unsigned char很好的捕捉,这是我的错误(见[basic.fundamental]p1)。我已经纠正了。>提供一个链接来解释什么是vtable,但链接应该是比维基百科更权威的东西。理想情况下,在我们的参考书目中引用。我认为其中一个与我们的定义有关,但似乎情况并非如此,因为我们对vtable没有定义。我已经纠正了。
亚伦·鲍尔曼 发表:
仅供参考:在本周的WG14会议上,我与David Keaton讨论了这是否仅适用于C,但事实并非如此。6.2.6.2 第 1 款和第 2 款的意图是处理 、、 和相同。unsigned charsigned charunsigned charchar
亚伦·鲍尔曼 发表:



