3 ms·
Some things that could make the functions fail: 1. In addition to stdin being closed (so gets(buf) would fail and continue execution without blocking), stdout/
by lhchavez 12y ago
Some things that could make the functions fail:
1. In addition to stdin being closed (so gets(buf) would fail and continue execution without blocking), stdout/stderr are line buffered when writing to a terminal stream, so they are implicitly flushed when there is a newline[1]. There is no newline, so it is possible for the text not to be printed.
2. strcpy/memcpy is not safe when the memory regions overlap since it might do optimizations that corrupt the string[2]. memmove is the right function to use.
3. Running the process with a non-writable working directory would raise an exception when trying to open the file for appending or create the file. Additionally, the example mentions web developers, so it probably means multiple threads/processes executing concurrently. There is no synchronization in place, so output might be corrupted (small writes might behave atomically, but larger ones will most likely be written in chunks, which introduces races).
[1]: http://stackoverflow.com/a/4201325/688337 http://stackoverflow.com/a/4201325/688337
[2]: http://linux.die.net/man/3/strcpy http://linux.die.net/man/3/strcpy
edit: formatting.
- Someone 12y agoThe third one also needs a try/finally to guarantee (for some definition of the word) that the file gets closed. With the code as-is, you can introduce errors elsewhere in the program by renaming the log file, creating a directory with the same name, waiting a while for the process to run out of file descriptors, and then deleting the directory and undoing the rename. Conversely, the function can throw if another part of the program keeps too many files open.