[英]Mapping external error codes to std::error_condition
我正在考慮修改MS結構化異常到異常映射代碼,我們必須使用新的C ++ 11 error_code / error_condition / exception mechanisim 。
我的理解是,一般的哲學是你應該首先嘗試將你的錯誤代碼映射到std :: error_condition代碼,否則,制作你自己的自定義error_condition代碼。
我看到的問題是std :: errc非常適合與POSIX錯誤配合使用。 如果我從一個源代碼中獲取代碼,該代碼具有與典型OS調用相比差異很大的錯誤,那么它就不能很好地映射。
例如,我們可以使用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?
那么攻擊它的最佳方法是什么?
首先,正如@JamesMcNellis評論的那樣,一些異常是非常危險的,讓操作系統處理它們並終止程序可能會更好,因為這些錯誤通常是代碼中的錯誤。 但是,您可能想要處理它們並編寫類似崩潰報告的內容,可能會使用堆棧和寄存器轉儲。
除此之外, std::error_condition
和std::error_code
不能用於處理POSIX
錯誤。 它們的結構設計方式可以處理任何等於0的int
值表示成功的情況,否則會出現錯誤,因此您可以編寫一個完全有效的代碼,將它們與std::error_code
和std::error_condition
但是您應該從std::error_category
驅動一個類並實現其虛函數,以提供與您的錯誤代碼匹配的錯誤代碼的說明(在您的情況下為NT狀態代碼):
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() );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.