参数验证Parameter Validation

大多数安全性增强的 CRT 函数和很多已有的函数都会验证其参数。Most of the security-enhanced CRT functions and many of the preexisting functions validate their parameters. 这可能包括检指针是否为 NULL、检查整数在有效的范围内或检查枚举该值是否有效。This could include checking pointers for NULL, checking that integers fall into a valid range, or checking that enumeration values are valid. 当发现无效参数时,将执行无效参数处理程序。When an invalid parameter is found, the invalid parameter handler is executed.

无效参数处理程序例程Invalid Parameter Handler Routine

当 C 运行时库函数检测到无效参数时,会捕获一些错误相关信息,然后调用一个宏来包装 _invalid_parameter_invalid_parameter_noinfo,或 _invalid_parameter_noinfo_noreturn 中的一个无效参数处理程序调度函数。When a C Runtime Library function detects an invalid parameter, it captures some information about the error, and then calls a macro that wraps an invalid parameter handler dispatch function, one of _invalid_parameter, _invalid_parameter_noinfo, or _invalid_parameter_noinfo_noreturn. 调用的调度函数取决于你的代码是否分别为调试版本、零售版本,或错误是否被视为不可恢复。The dispatch function called depends on whether your code is, respectively, a debug build, a retail build, or the error is not considered recoverable.

在调试版本中,在调用调度函数之前,无效参数宏通常会引发失败断言和调试器断点。In Debug builds, the invalid parameter macro usually raises a failed assertion and a debugger breakpoint before the dispatch function is called. 执行代码时,可能会向具有“中止”、“重试”和“继续”或类似选项的对话框中的用户报告断言,具体取决于操作系统和运行时库版本。When the code is executed, the assertion may be reported to the user in a dialog box that has "Abort", "Retry", and "Continue" or similar choices, depending on the operating system and runtime library version. 这些选项允许用户立即终止程序、附加调试器,或允许调用调度函数的现有代码继续运行。These options allow the user to immediately terminate the program, to attach a debugger, or to let the existing code continue to run, which calls the dispatch function.

无效参数处理程序调度函数反过来调用当前分配的无效参数处理程序。The invalid parameter handler dispatch function in turn calls the currently assigned invalid parameter handler. 默认情况下,无效参数调用 _invoke_watson 将导致应用程序“故障”,也就是说,终止和生成小型转储。By default, the invalid parameter calls _invoke_watson which causes the application to "crash," that is, terminate and generate a mini-dump. 如果由操作系统启用,对话框会询问用户是否想要将故障转储加载到 Microsoft 进行分析。If enabled by the operating system, a dialog box asks the user if they want to load the crash dump to Microsoft for analysis.

此行为可以通过使用函数 _set_invalid_parameter_handler _set_thread_local_invalid_parameter_handler 进行更改,将无效参数处理程序设置为你自己的函数。This behavior can be changed by using the functions _set_invalid_parameter_handler or _set_thread_local_invalid_parameter_handler to set the invalid parameter handler to your own function. 如果指定的函数未终止应用程序,控制权就会返回至已接收无效参数的函数处。If the function you specify does not terminate the application, control is returned to the function that received the invalid parameters. 在 CRT 中,这些函数通常会停止函数执行,向错误代码设置 errno,并返回错误代码。In the CRT, these functions will normally cease function execution, set errno to an error code, and return an error code. 在很多情况下,errno 值和返回值都是 EINVAL,这指示参数无效。In many cases, the errno value and the return value are both EINVAL, indicating an invalid parameter. 有时候会返回更具体的错误代码,如作为参数传递的错误的文件指针的 EBADFIn some cases, a more specific error code is returned, such as EBADF for a bad file pointer passed in as a parameter. 有关 errno 的详细信息,请参阅 errno、_doserrno、_sys_errlist 和 _sys_nerrFor more information on errno, see errno, _doserrno, _sys_errlist, and _sys_nerr.

另请参阅See Also

CRT 中的安全功能 Security Features in the CRT
CRT 库功能CRT Library Features