有没有一种好方法可以在 C++ 中销毁调用者的堆栈?
Is there a good way to destroy caller's stack in C++?
练习 C++ 的程序员确实知道一些技巧,例如 "Scope Guard",也许其他技巧涉及对临时对象的引用。
我不是练习 C++ 的程序员,但我想问(出于好奇)第三方库是否有可能以某种方式损害调用者的堆栈。可能涉及突然的析构函数或某种其他范围的生命周期魔法?
way third party library could harm the callers' stack
每当来自第三方库的代码运行时——无论是 OS 加载程序知道要调用的动态加载库的初始化例程,还是来自客户端应用程序代码的显式调用——通常(在大多数情况下) OSes'/implementations' 安全模型)与客户端应用程序本身一样具有破坏堆栈(或任何其他内存)的能力;例如:
void library_code()
{
char x;
char* p = &x;
*(p - 2) = 23; // undefined behaviour - may do nothing or anything,
// but usually overwrites stack or SIGSEGVs
*(p + 54) = 99; // stacks might grow up or down, so may have to + or -
// from &x to address in-use stack memory...
}
练习 C++ 的程序员确实知道一些技巧,例如 "Scope Guard",也许其他技巧涉及对临时对象的引用。
我不是练习 C++ 的程序员,但我想问(出于好奇)第三方库是否有可能以某种方式损害调用者的堆栈。可能涉及突然的析构函数或某种其他范围的生命周期魔法?
way third party library could harm the callers' stack
每当来自第三方库的代码运行时——无论是 OS 加载程序知道要调用的动态加载库的初始化例程,还是来自客户端应用程序代码的显式调用——通常(在大多数情况下) OSes'/implementations' 安全模型)与客户端应用程序本身一样具有破坏堆栈(或任何其他内存)的能力;例如:
void library_code()
{
char x;
char* p = &x;
*(p - 2) = 23; // undefined behaviour - may do nothing or anything,
// but usually overwrites stack or SIGSEGVs
*(p + 54) = 99; // stacks might grow up or down, so may have to + or -
// from &x to address in-use stack memory...
}