set_constraint_handler_s, constraint_handler_t
來自 cppreference.com
在標頭檔案 <stdlib.h> 中定義 |
||
constraint_handler_t set_constraint_handler_s( constraint_handler_t handler ); |
(1) | (C11 起) |
typedef void (*constraint_handler_t)( const char* restrict msg, void* restrict ptr, |
(2) | (C11 起) |
2) 當執行時違反約束時將被呼叫的處理程式的指標。
如果從未呼叫 set_constraint_handler_s
,則預設處理程式是實現定義的:它可能是 abort_handler_s、ignore_handler_s 或其他實現定義的處理程式。
與所有邊界檢查函式一樣,只有當實現定義了__STDC_LIB_EXT1__並且使用者在包含<stdlib.h>之前將__STDC_WANT_LIB_EXT1__定義為整數常量1時,才能保證set_constraint_handler_s
和constraint_handler_t
可用。
目錄 |
[編輯] 引數
處理程式 | - | 指向型別為constraint_handler_t 的函式的指標或空指標 |
msg | - | 指向描述錯誤的字串的指標 |
ptr | - | 指向實現定義物件或空指標的指標。實現定義物件的示例是提供檢測到違規的函式名稱和檢測到違規的行號的物件 |
error | - | 即將由呼叫函式返回的錯誤,如果它恰好是返回errno_t 的函式之一 |
[編輯] 返回值
指向先前安裝的執行時約束處理程式的指標。(注意:此指標永遠不會是空指標,因為呼叫set_constraint_handler_s(NULL)會設定系統預設處理程式)。
[編輯] 示例
執行此程式碼
#define __STDC_WANT_LIB_EXT1__ 1 #include <string.h> #include <stdio.h> #include <stdlib.h> int main(void) { #ifdef __STDC_LIB_EXT1__ char dst[2]; set_constraint_handler_s(ignore_handler_s); int r = strcpy_s(dst, sizeof dst, "Too long!"); printf("dst = \"%s\", r = %d\n", dst, r); set_constraint_handler_s(abort_handler_s); r = strcpy_s(dst, sizeof dst, "Too long!"); printf("dst = \"%s\", r = %d\n", dst, r); #endif }
可能的輸出
dst = "", r = 22 abort_handler_s was called in response to a runtime-constraint violation. The runtime-constraint violation was caused by the following expression in strcpy_s: (s1max <= (s2_len=strnlen_s(s2, s1max)) ) (in string_s.c:62) Note to end users: This program was terminated as a result of a bug present in the software. Please reach out to your software's vendor to get more help. Aborted
[編輯] 參考資料
- C23 標準 (ISO/IEC 9899:2024)
- K.3.6/2 constraint_handler_t (p: TBD)
- K.3.6.1.1 set_constraint_handler_s 函式 (p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- K.3.6/2 constraint_handler_t (p: TBD)
- K.3.6.1.1 set_constraint_handler_s 函式 (p: TBD)
- C11 標準 (ISO/IEC 9899:2011)
- K.3.6/2 constraint_handler_t (p: 604)
- K.3.6.1.1 set_constraint_handler_s 函式 (p: 604-605)
[編輯] 另請參閱
(C11) |
邊界檢查函式的中止回撥。 (函式) |
(C11) |
邊界檢查函式的忽略回撥。 (函式) |