☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

memset 等函数不应作用于非 POD 对象

13.4 ID_nonPODFilling
目录 › next › previous

memset、memcpy、memmove 等具有填充功能的函数不应作用于非“POD 类型”的对象,否则会破坏其数据的内在关系。

本规则是 ID_nonPODBinaryCast 的特化。

示例:

class A {
    ....
public:
    virtual ~A();
};

void foo(A& a) {
    memset(&a, 0, sizeof(a));   // Non-compliant, the ‘vfptr’ is corrupted
}

例中 memset 填充非 POD 对象,其虚函数表指针会被破坏,造成严重的运行时错误。

相关

ID_nonPODBinaryCast

参考

CWE-463 C++ Core Guidelines SL.con.4 C++ Core Guidelines C.90
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.