set_constraint_handler_s, constraint_handler_t
|
定义于头文件
<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 起) |
如果从未调用
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
可用。
目录 |
参数
| handler | - |
指向
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 (页: TBD)
-
- K.3.6.1.1 set_constraint_handler_s 函数 (页: TBD)
- C17 标准 (ISO/IEC 9899:2018):
-
- K.3.6/2 constraint_handler_t (页: TBD)
-
- K.3.6.1.1 set_constraint_handler_s 函数 (页: TBD)
- C11 标准 (ISO/IEC 9899:2011):
-
- K.3.6/2 constraint_handler_t (页码: 604)
-
- K.3.6.1.1 set_constraint_handler_s 函数 (页码: 604-605)
参见
|
(C11)
|
边界检查函数的中止回调
(函数) |
|
(C11)
|
边界检查函数的忽略回调
(函数) |