set_constraint_handler_s, constraint_handler_t
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <stdlib.h> で定義
|
||
constraint_handler_t set_constraint_handler_s( constraint_handler_t handler ); |
(C11以上) | |
すべての境界チェック付き関数によって実行時制約違反時に呼ばれるハンドラを設定し、または (handler がヌルポインタの場合は) デフォルトのハンドラを復元します。
ハンドラは以下のように定義される constraint_handler_t 型の関数を指すポインタでなければなりません。
| ヘッダ <stdlib.h> で定義
|
||
typedef void (*constraint_handler_t)( const char *restrict msg, void *restrict ptr, errno_t error); |
(C11以上) | |
実行時制約違反が発生すると、以下の引数でハンドラが呼ばれます。
1) エラーを記述する文字列を指すポインタ。
2) 処理系定義のオブジェクトを指すポインタまたはヌルポインタ。 処理系定義のオブジェクトの例としては、違反を検出した関数の名前や、違反が検出されたときの行番号などです。
3)
errno_t を返す関数で発生した場合、その関数が返そうとしているエラー。set_constraint_handler_s が一度も呼ばれない場合、デフォルトのハンドラは処理系定義です。 abort_handler_s、 ignore_handler_s、または何らかの他の処理系定義のハンドラになるかもしれません。
- すべての境界チェック付き関数と同様に、
set_constraint_handler_sおよびconstraint_handler_tは__STDC_LIB_EXT1__が処理系によって定義されていて、<stdlib.h>をインクルードする前にユーザが__STDC_WANT_LIB_EXT1__を整数定数 1 に定義した場合にのみ、利用可能であることが保証されます。
引数
| handler | - | constraint_handler_t 型の関数を指すポインタ、またはヌルポインタ
|
戻り値
以前に設定されていた実行時制約ハンドラを指すポインタ (ノート: set_constraint_handler_s(NULL) の呼び出しはシステムのデフォルトハンドラをセットアップするため、このポインタはヌルポインタになることはありません)。
例
Run this code
#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
参考文献
- C11 standard (ISO/IEC 9899:2011):
- K.3.6/2 constraint_handler_t (p: 604)
- K.3.6.1.1 The set_constraint_handler_s function (p: 604-605)
関連項目
(C11) |
境界チェック関数の異常終了コールバック (関数) |
(C11) |
境界チェック関数の無視コールバック (関数) |