5

我正在考虑修改我们必须使用新的 C++11 error_code/error_condition/exception mechanisim的 MS 结构化异常到异常映射代码。

我的理解是,一般理念是您应该首先尝试将错误代码映射到 std::error_condition 代码,否则,请制作自己的自定义 error_condition 代码。

我看到的问题是std::errc非常适合处理 POSIX 错误。如果我从具有与典型操作系统调用完全不同的错误范围的源获取代码,则它的映射不正确。

例如,让我们以Microsoft 的 SEH 代码为例。这些来自操作系统,因此理论上它应该映射以及 POSIX 之外的任何内容。但它肯定似乎根本无法很好地映射:

EXCEPTION_ACCESS_VIOLATION  = permission_denied
EXCEPTION_ARRAY_BOUNDS_EXCEEDED = argument_out_of_domain perhaps?
EXCEPTION_BREAKPOINT = ?
EXCEPTION_DATATYPE_MISALIGNMENT = ?
EXCEPTION_FLT_DENORMAL_OPERAND = ? 
EXCEPTION_FLT_DIVIDE_BY_ZERO = ?
EXCEPTION_FLT_INEXACT_RESULT = ? 
EXCEPTION_FLT_INVALID_OPERATION = ?
EXCEPTION_FLT_OVERFLOW = ?
EXCEPTION_FLT_STACK_CHECK = ?
EXCEPTION_FLT_UNDERFLOW = ?
EXCEPTION_GUARD_PAGE = ?
EXCEPTION_ILLEGAL_INSTRUCTION = ?
EXCEPTION_IN_PAGE_ERROR = ?
EXCEPTION_INT_DIVIDE_BY_ZERO = ?
EXCEPTION_INT_OVERFLOW = value_too_large perhaps, but then what do I use for _STACK_OVERFLOW?
EXCEPTION_INVALID_DISPOSITION = ?
EXCEPTION_INVALID_HANDLE = ? 
EXCEPTION_NONCONTINUABLE_EXCEPTION = ? 
EXCEPTION_PRIV_INSTRUCTION = ?
EXCEPTION_SINGLE_STEP = ?
EXCEPTION_STACK_OVERFLOW = value_too_large perhaps, but then what do I use for _INT_OVERFLOW?

那么攻击这个的最好方法是什么?

4

1 回答 1

2

First as commented by @JamesMcNellis some of this exceptions are very dangerous and it may be better to let OS handle them and terminate your program, since those errors are usually an error in your code. But may be you want to handle them and write something like a crash report possibly with a dump of stack and registers.

Beside that std::error_condition and std::error_code is not designed to only work with POSIX errors. Their structure is designed in a way that can handle any case that an int value that is equal to 0 indicate a success and otherwise an error, so you may write a completely valid code that use them with std::error_code and std::error_condition but you should drive a class from std::error_category and implement its virtual functions to provide explanation of your error codes that matched with your error codes(in your case NT status codes):

class NT_status_code_error_category : std::error_category {
public:
    const char* name() const {return "NT status code";}
    std::string message( int errCode ) const {
        switch( errCode ) {
        case EXCEPTION_ACCESS_VIOLATION: return "Access violation";
        // a couple of other error codes will be handled here
        default: return "Unknown status code";
        }
    }
    std::error_condition default_error_condition( int errCode ) const {
    return std::error_condition( errCode, *this );
}
};
inline NT_status_code_error_category const& NT_status_code_category() {
    static NT_status_code_error_category res;
    return res;
}

inline std::error_code make_NT_status_error_code( int status ) {
    return std::error_code( status, NT_status_code_category() );
}
inline std::error_condition make_NT_status_error_condition( int status ) {
    return std::error_condition( status, NT_status_code_category() );
}
于 2012-10-26T20:17:45.267 回答