?
Ce document utilise Manuel du site Web PHP chinois Libérer
在頭文件<stdlib.h>中定義 | ||
---|---|---|
constraint_handler_t set_constraint_handler_s(constraint_handler_t handler); | (自C11以來) |
配置要在運行時約束沖突時由所有邊界檢查函數(shù)調(diào)用的處理程序或恢復默認處理程序(如果handler
是空指針)。
處理程序必須是指向類型函數(shù)的指針constraint_handler_t
,該類型被定義為。
在頭文件<stdlib.h>中定義 | ||
---|---|---|
typedef void(* constraint_handler_t)(const char * restrict msg,void * restrict ptr,errno_t error); | (自C11以來) |
在運行時約束沖突中,使用以下參數(shù)調(diào)用它:
1)指向描述錯誤的字符串的指針
2)指向實現(xiàn)定義的對象或空指針的指針。實現(xiàn)定義對象的示例是對象,該對象給出檢測到違規(guī)的函數(shù)的名稱以及檢測到違規(guī)時的行號
3)調(diào)用函數(shù)返回的錯誤,如果它恰好是返回的函數(shù)之一 errno_t
如果set_constraint_handler_s
是從來不叫,默認的處理程序是實現(xiàn)定義的:它可能是abort_handler_s
,ignore_handler_s
或其他一些實現(xiàn)定義的處理程序。如同所有的邊界檢查功能,set_constraint_handler_s
并且constraint_handler_t
如果僅保證可供__STDC_LIB_EXT1__
由實現(xiàn)所定義,并且如果用戶定義__STDC_WANT_LIB_EXT1__
的整數(shù)常數(shù)1
,包括之前<stdlib.h>
。
handler | - | 指向constraint_handler_t類型的函數(shù)的指針或空指針 |
---|
指向先前安裝的運行時約束處理程序的指針。(注意:該指針永遠不是空指針,因為調(diào)用會set_constraint_handler_s(NULL)
設置系統(tǒng)默認處理程序)。
#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 = 22abort_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 resultof a bug present in the software. Please reach out to your software's vendor to get more help.Aborted
C11標準(ISO/IEC 9899:2011):
K.3.6/2 constraint_handler_t(p:604)
K.3.6.1.1 set_constraint_handler_s函數(shù)(p:604-605)
abort_handler_s(C11) | 取消對邊界檢查函數(shù)的回調(diào)(函數(shù)) |
---|---|
ignore_handler_s(C11) | 忽略邊界檢查函數(shù)的回調(diào)(函數(shù)) |