News for hackers. Stuff that matters.
Re:Dangerous Embedded C Coding Standard Rules (2011) by anywherenotes on Monday December 30, 2013 @02:20PM (#6986262)
The code I used is more like this:

  if ((f = open_file()) == NULL)
  {
    RETURN_ERROR(OPEN_FILE_ERROR);
  }
  if ((s == allocate_string()) == NULL)
  {
    RETURN_ERROR(ALLOCATE_STRING_ERROR);
  }
  if ...
And RETURN_ERROR is defined as:

  #define RETURN_ERROR(my_error) { \
    if (f != NULL) { fclose(f); f = NULL; } \
    if (s != NULL) { free(s); s = NULL; } \
    ...
    return (my_error); \
  }
Re:Dangerous Embedded C Coding Standard Rules (2011) by nitrogen on Monday December 30, 2013 @04:24PM (#6987065)
The approach I use, and that used many places in the Linux kernel, is to use goto for error handling:

  if(failure_1) {
      goto error;
  }
  if(failure_2) {
      goto error;
  }
  
  // Do stuff
  return 0;
  
  error:
  if(f != NULL) {
      fclose(f);
  }
  if(s != NULL) {
      free(s);
  }
  return -1;