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

供语言机制调用的函数不符合要求

C++-Undefined-Behavior-78
目录 › next › previous

如 replacement functions 或 handler functions 不符合要求(required behavior),会导致未定义的行为。

示例:

struct T {
    void* operator new(size_t) {  // A replacement function
        return nullptr;           // Undefined behavior
    }
};

标准要求 operator new 返回非空地址,分配失败则抛出 bad_alloc 异常(见 ISO/IEC 14882:2011 18.6.1.1),否则导致未定义的行为。

又如:

void my_handler() {  // A handler function
    return;          // Do nothing
}

int main() {
    set_terminate(my_handler);   // Undefined behavior
    ....
}

标准要求 terminate handler 结束进程的执行,不可返回至调用方(见 ISO/IEC 14882:2011 18.8.3.1),否则导致未定义的行为。

依据

ISO/IEC 14882:2003 17.1.15-undefined ISO/IEC 14882:2011 17.3.21-undefined
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.