Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fixed the C11 CS so it uses fopen_s and properly orders the strerror_s parameters.

...

Note that Linux provides two versions of strerror_r(), known as the XSI-compliant version and the GNU-specific version. This compliant solution assumes the XSI-compliant version. You can get the XSI-compliant version if you compile applications in the way POSIX requires (that is, by defining _POSIX_C_SOURCE or _XOPEN_SOURCE appropriately). Check your strerror_r() manual page to see which version(s) are available on your system.

Compliant Solution (

...

C11, strerror_s()

This compliant solution uses the strerror_s() function from Annex K of the C Standard, which has the same functionality as strerror() but guarantees thread-safety. Furthermore, in C11, errno is a thread-local variable, so there is no race condition between the time it is initialized and the time it is read by strerror_s().

Code Block
bgColor#ccccff
langc
errnoFILE* f = 0;
FILE* fderrno_t err = fopen_s(&f, filename, "r");
if (fd == NULLerr) {
  char errmsg[BUFSIZ];
  if (strerror_s(errnoerrmsg, errmsgBUFSIZ, BUFSIZerr) != 0) {
    /* handle error */
  }
  printf("Could not open file because of %s\n", errmsg);
}

...