Versions Compared

Key

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

...

Code Block
bgColor#ffcccc
langc
void func(char* name) {
  char* s = NULL;
  asprintf(&s,"Hello, %s!\n", name);
  (void) puts(s);
  free(s);
}

Compliant Solution

This compliant solution checks to make sure no error occurred.

Code Block
bgColor#ccccff
langc
void func(char* name) {
  char* s = NULL;
  if (asprintf(&s,"Hello, %s!\n", name) < 0) {
    /* Handle error */
  }
  (void) puts(s);
  free(s);
} 

Exceptions

EXP12-C-EX1: If the return value is inconsequential or if any errors can be safely ignored, such as for functions called because of their side effects, the function should be explicitly cast to void to signify programmer intent. For an example of this exception, see "Compliant Solution (Remove Existing Destination File)" under the section "Portable Behavior" in FIO10-C. Take care when using the rename() function, or Exception ERR33-C-EX1 in ERR33-C. Detect and handle standard library errors.

...