Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Section 6.6.1 [ISO/IEC TR 24731-1:2007] states:

When the handler is called, it is passed the following arguments in the following order:

  1. A pointer to a character string describing the runtime constraint violation.
  2. A null pointer or a pointer to an implementation-defined object.
  3. If the function calling the handler has a return type declared as errno_t, the return value of the function is passed. Otherwise, a positive value of type errno_t is passed.

The implementation has a default constraint handler that is used if no calls to the set_constraint_handler_s() function have been made or the handler argument to set_constraint_handler_s() is a null pointer. The behavior of the default handler is implementation-defined, and it may cause the program to exit or abort.

And Section 6.1.4 [ISO/IEC TR 24731-1:2007] states:

The runtime constraint handler might not return. If the handler does return, the library function whose runtime constraint was violated shall return some indication of failure as given by the returns section in the function's specification.

These runtime constraint handlers mitigate some of the potential insecurity caused by in-band error indicators. (See recommendation ERR02-C. Avoid in-band error indicators.)

...

Code Block
bgColor#FFCCCC
langc

errno_t function(char *dst1, size_t size){
  char src1[100] = "hello";

  if (strcpy_s(dst1, size, src1) != 0) {
    return -1;
  }
  /* ... */
  return 0;
}

This results in inconsistent The result is 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 It is therefore prudent to explicitly install a runtime constraint handler to ensure consistent behavior across implementations.

...

This compliant solution explicitly installs a runtime constraint handler by invoking the set_constraint_handler_s() function. This It would typically be performed during system initialization and before any functions that used the mechanism were invoked.

Code Block
bgColor#ccccff
langc

constraint_handler_t handle_errors(void) {
  /* Handle 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;
}

...

Code Block
bgColor#ccccff
langc

_invalid_parameter_handler handle_errors(
   const wchar_t* expression,
   const wchar_t* function,
   const wchar_t* file,
   unsigned int line,
   uintptr_t pReserved
) {
  /* Handle 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;
}

...