emikulic / darkhttpd

When you need a web server in a hurry.
https://unix4lyfe.org/darkhttpd/
ISC License
1.03k stars 83 forks source link

Use of stdout after fclose() #64

Open g-rden opened 6 months ago

g-rden commented 6 months ago

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:

hhartzer commented 6 months ago

Interesting. Maybe we should have some kind of logger function? Not sure how to best handle this, or if we should ever close stdout.

g-rden commented 6 months ago

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.