Use of stdout after fclose()
logfile, which by default is stdout, gets closed https://github.com/emikulic/darkhttpd/blob/64fe4ccbcf89e1f7f1e7b61954ff60d6404d636c/darkhttpd.c#L2994 and later we want to write to it https://github.com/emikulic/darkhttpd/blob/64fe4ccbcf89e1f7f1e7b61954ff60d6404d636c/darkhttpd.c#L3030-L3037
This results in undefined behavoir: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html
I am pretty sure that it should be if (logfile_name != NULL) fclose(logfile); anyway. If logfile where NULL, the program would have exited already. But since this changes what gets printed to console, I open this issue instead of a PR
There are multiple options:
- Don't close logfile if it's stdout, but then the usage stats get printed by default, which was not the case before.
- Close logfile even if it's stdout and only print usage stats when logfile was not stdout (logfile_name is not NULL). This is like the current output.
- Close logfile even if it's stdout and print usage stats to stderr.
Interesting. Maybe we should have some kind of logger function? Not sure how to best handle this, or if we should ever close stdout.
There is a logger function that logs to the mentioned logfile. The unintentional thing is that logfile by default is set stdout.
We could also move the usage stats above fclose(), so stdout does not get used after potentially closing it.