名前空間
変種
操作

set_constraint_handler_s, constraint_handler_t

From cppreference.com
< c‎ | エラー
ヘッダー <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,

                                      errno_t error );
(2) (C11 以降)
1) すべての 境界チェック関数で、実行時制約違反時に呼び出されるハンドラを設定します。あるいは、デフォルトのハンドラを復元します(handlerがヌルポインタの場合)。
2) 実行時制約違反時に呼び出されるハンドラへのポインタ。

set_constraint_handler_s が一度も呼び出されない場合、デフォルトのハンドラは実装定義です。abort_handler_signore_handler_s、またはその他の実装定義のハンドラである可能性があります。

すべての境界チェック関数と同様に、set_constraint_handler_sconstraint_handler_t は、実装によって __STDC_LIB_EXT1__ が定義されており、かつユーザーが <stdlib.h> をインクルードする前に __STDC_WANT_LIB_EXT1__ を整数定数 1 として定義している場合にのみ、利用が保証されます。

目次

[編集] パラメータ

ハンドラ - 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 The set_constraint_handler_s function (p: TBD)
  • C17標準 (ISO/IEC 9899:2018)
  • K.3.6/2 constraint_handler_t (p: TBD)
  • K.3.6.1.1 The set_constraint_handler_s function (p: TBD)
  • C11標準 (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)

[編集] 関連項目

境界検査付き関数のabortコールバック
(関数) [編集]
境界検査付き関数のignoreコールバック
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)