What (can) happen if I use fclose () when fopen () fails
Here's my relatively simple scenario, and I'm wondering if I can save the conditional code myself and tidy it up. Its production code that someone wrote and I just clean it up. It didn't have fclose at all, so all I added was fclose () conditional lines:
FILE *fp;
struct stat sb;
/* snipped code */
if (((fp = fopen (config_file, "r+")) == NULL) || (fstat (fileno (fp), &sb))) {
syslog (LOG_ERR, "Fault. Unable to read config file");
if (fp != NULL) {
fclose (fp);
}
return -1;
}
/* code carries on after this */
The question is, do I really need to have if(fp != null)
in my code? What are the implications of simple execution fclose(fp)
without validation? I read the C89 standard, but it was not clear to me what the results would be.
Greetings in advance
Steve
+3
source to share
2 answers