检测对堆栈变量的调用delete(使用强制转换到指针运算符)
Detect calling delete on stack variables (with cast-to-pointer operator)
tldr
有没有一种方法可以静态地检测以下代码中的错误?
struct Foo
{
operator const int*()
{
return &data;
}
int data;
};
int main()
{
Foo f;
delete f;
}
由于从Foo到void*的转换只包含一个用户定义的转换,因此实际上允许在f
上调用delete
。
更长的故事
在我们的代码库中,有一种非常愚蠢的反序列化字符串的方法,即伪代码
char * buff = bar.loadString();
use buff;
delete buff;
方法被更改为一个模板化的加载函数,所以现在反序列化看起来像
bar.load(m_IntMember);
bar.load(m_StringMember);
但loadString
的所有出现(有很多(都必须手动更改,如下所示:
string buff;
bar.load(buff);
use buff;
我们都知道人为错误会导致什么,所以有些地方的代码被错误地修改了,比如
string buff;
bar.load(buff);
use buff;
delete buff; //notice the delete
由于我们使用的是string
的一个非标准实现,它实际上有一个overloaded const char * operator
,它可以被广播到void*
,可以被删除。。。
我想在编译时捕捉所有这些错误(我们有自定义的高性能分配器,所以在运行时,很容易损坏内存而不会出现任何运行时错误(
我无法声明全局删除运算符接受const char*
我无法暂时从string
中删除delete运算符,因为它被大量使用,所以没有它就无法编译(我无法从所有错误中"过滤掉"所描述的错误,因为msvc在达到一定的错误量时停止编译(
我能做什么?
这似乎做到了:
#include <iostream>
struct Foo
{
Foo() : data(42) {}
operator const int*()
{
return &data;
}
int data;
struct AmbiguousPointerConversion {};
private: operator AmbiguousPointerConversion *() {
throw "hi";
}
};
int main()
{
Foo f;
const int *p = f;
std::cout << *p << 'n';
delete f;
}
用g++编译这个给了我:
try.cc: In function ‘int main()’:
try.cc:25:12: error: ambiguous default type conversion from ‘Foo’
delete f;
^
try.cc:25:12: note: candidate conversions include ‘Foo::operator const int*()’ and ‘Foo::operator Foo::AmbiguousPointerConversion*()’
try.cc:25:12: error: type ‘struct Foo’ argument given to ‘delete’, expected pointer
相关文章:
- 转换指针引用的字符串
- 无法向上转换指针到指针参数
- 混合转换指针和引用
- 堆栈对象的强制转换指针
- 基类到派生模板类的强制转换指针,而不知道类型
- 删除类型转换指针的最佳方法
- 管理到本机值类转换:强制转换指针是否安全?
- 转换指针类型
- 为什么函数不能正确强制转换指针(从基类到派生类)
- 当我们递增下面的类型转换指针时会发生什么?
- 类型强制转换指针构造函数调用
- 如何转换(指针向量)-->(指向指针数组的指针)
- 如何从类功能转换指针
- 在C++对象中:我应该使用父类强制转换指针,还是应该使用实际类本身进行强制转换
- Delphi中的类型转换指针添加
- C++分段错误(核心转储)错误 - 强制转换指针/将函数值返回到线程
- 以C++和运算符优先级键入指向数组成员的强制转换指针
- C++动态强制转换指针的内存清理
- 转换指针和引用的好处
- 在c++中转换指针