2 ms·
all the reddit users from the linked article missed the SIGPIPE trick! you don't need to check the return value from write() as your process will be terminated
by blibble 9y ago
all the reddit users from the linked article missed the SIGPIPE trick!
you don't need to check the return value from write() as your process will be terminated with SIGPIPE if it tries writing to a closed pipe.
saying that, none of them check the return code correctly: if the consumer only reads a byte at a time you could eventually get 'yyyyyyyyyyyyyyyyyy' (without any newlines)
quite impressive that so many implementations of "yes" have the same bug :)
- dmitrygr 9y agoplease demonstrate that consumer. on linux there is no way to do this without your consumer being a kernel driver that actually [purposefully accepts one byte at a time. all file io and pipes have basically no way to accept one byte only (except EOF)
- blibble 9y agoyou wouldn't see it on Linux as pipes are implemented using complete pages (always powers of 2), but there's probably some OS out there with a different implementation where you can set the buffer size to be an odd number, and then you'd see the bug with plain simple cat I didn't say you'd likely see it in practice :)
- caf 9y agoYou can't rely on SIGPIPE to terminate it or your program will fail to terminate on error if its output is redirected to a disk file.
- electrum 9y agowrite(2) is a blocking call (unless O_NONBLOCK is set), so you should never have a partial write while the descriptor is connected to something valid.