?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
在頭文件<stdlib.h>中定義 | ||
---|---|---|
constraint_handler_t set_constraint_handler_s(constraint_handler_t handler); | (自C11以來(lái)) |
配置要在運(yùn)行時(shí)約束沖突時(shí)由所有邊界檢查函數(shù)調(diào)用的處理程序或恢復(fù)默認(rèn)處理程序(如果handler
是空指針)。
處理程序必須是指向類(lèi)型函數(shù)的指針constraint_handler_t
,該類(lèi)型被定義為。
在頭文件<stdlib.h>中定義 | ||
---|---|---|
typedef void(* constraint_handler_t)(const char * restrict msg,void * restrict ptr,errno_t error); | (自C11以來(lái)) |
在運(yùn)行時(shí)約束沖突中,使用以下參數(shù)調(diào)用它:
1)指向描述錯(cuò)誤的字符串的指針
2)指向?qū)崿F(xiàn)定義的對(duì)象或空指針的指針。實(shí)現(xiàn)定義對(duì)象的示例是對(duì)象,該對(duì)象給出檢測(cè)到違規(guī)的函數(shù)的名稱(chēng)以及檢測(cè)到違規(guī)時(shí)的行號(hào)
3)調(diào)用函數(shù)返回的錯(cuò)誤,如果它恰好是返回的函數(shù)之一 errno_t
如果set_constraint_handler_s
是從來(lái)不叫,默認(rèn)的處理程序是實(shí)現(xiàn)定義的:它可能是abort_handler_s
,ignore_handler_s
或其他一些實(shí)現(xiàn)定義的處理程序。如同所有的邊界檢查功能,set_constraint_handler_s
并且constraint_handler_t
如果僅保證可供__STDC_LIB_EXT1__
由實(shí)現(xiàn)所定義,并且如果用戶定義__STDC_WANT_LIB_EXT1__
的整數(shù)常數(shù)1
,包括之前<stdlib.h>
。
handler | - | 指向constraint_handler_t類(lèi)型的函數(shù)的指針或空指針 |
---|
指向先前安裝的運(yùn)行時(shí)約束處理程序的指針。(注意:該指針永遠(yuǎn)不是空指針,因?yàn)檎{(diào)用會(huì)set_constraint_handler_s(NULL)
設(shè)置系統(tǒng)默認(rèn)處理程序)。
#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標(biāo)準(zhǔn)(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) | 取消對(duì)邊界檢查函數(shù)的回調(diào)(函數(shù)) |
---|---|
ignore_handler_s(C11) | 忽略邊界檢查函數(shù)的回調(diào)(函數(shù)) |