Stdio.write_file return value handling broken
Imported from http://bugzilla.roxen.com/bugzilla/show_bug.cgi?id=6052
Reported by Per Cederqvist ceder@lysator.liu.se
Stdio.write_file is documented to return the number of bytes written. However, it actually returns the number of bytes written by the last call to File()->write.
The do...while loop is broken. It works fine on the first and second run, but on the third run it will write garbage to the file. This is the loop:
do {
ret=f->write(str[ret..]);
if (ret < 0)
error ("Couldn't write to %O: %s\n", filename, strerror (f->errno()));
} while (ret < sizeof (str));
Consider the case where str is 1000 bytes, and f->write() returns 100 on each call. On the first run, the first 100 bytes of str is written. On the next iteration, bytes 100..199 are written. On the third iteration, bytes 100..199 are written again. Not good. :-)
I doubt that this is much of a problem in practice. It could fail if the disk is full so that the first f->write() cannot write the entire string, but becomes free so that the second f->write() can write some data. Possible, but not very likely. At least on Linux signals seems to be blocked so that sending a SIGSTOP does not interrupt the write(2) system call. If that is not true on all platforms, there is a real problem here.