3 ms·
The code I used is more like this: if ((f = open_file()) == NULL) { RETURN_ERROR(OPEN_FILE_ERROR); } if ((s == allocate_string()) == NULL) {
by anywherenotes 13y ago
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); \
}
- nitrogen 13y agoThe 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;