...
Noncompliant Code Example (TR24731-1)
In this noncompliant code example, the strcpy_s()
function is called, but no runtime-constraint handler has been explicitly registered. As a result, the implementation-defined default handler will be called on a runtime error.
...
This will result in inconsistent behavior across implementations and possible termination of the program instead of a graceful exit. The implementation-defined default handler performs a default action consistent with a particular implementation. However, this may not be the desired action, and because the behavior is implementation-defined, it is not guaranteed to be the same on all implementations.
As a result, it is generally prudent to explicitly install a runtime constraint handler to ensure consistent behavior across implementations.
...
Code Block | ||
---|---|---|
| ||
constraint_handler_t handle_errors(void) { /* handleHandle runtime constraint error */ } /*...*/ set_constraint_handler_s(handle_errors); /*...*/ /* Returns zero on success */ errno_t function(char *dst1, size_t size){ char src1[100] = "hello"; if (strcpy_s(dst1, size, src1) != 0) { return -1; } /* ... */ return 0; } |
Compliant Code Example (Visual Studio 2008/.NET Framework 3.5)
Unfortunately, although Although the ISO/IEC TR 24731-1 functions were created by Microsoft, currently available versions of Microsoft Visual Studio do not support the same interface defined by the TR technical report for installing runtime constraint handlers. Visual Studio calls these functions "invalid parameter handlers," and they are installed by calling the _set_invalid_parameter_handler()
function. The signature of the handler is also significantly different.
Code Block | ||
---|---|---|
| ||
_invalid_parameter_handler handle_errors( const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved ) { /* handleHandle invalid parameter */ } /*...*/ _set_invalid_parameter_handler(handle_errors) /*...*/ errno_t function(char *dst1, size_t size) { char src1[100] = "hello"; if (strcpy_s(dst1, size, src1) != 0) { return -1; } /* ... */ return 0; } |
...