]> granicus.if.org Git - nethack/commitdiff
Avoid panic on zero-byte writes to save files
authorAlex Smith <ais523@nethack4.org>
Tue, 1 Mar 2022 13:23:10 +0000 (13:23 +0000)
committerAlex Smith <ais523@nethack4.org>
Tue, 1 Mar 2022 13:23:10 +0000 (13:23 +0000)
This is a well-defined operation, so bwrite() should be able to
handle it. However, when running under glibc, fwrite() produces an
unexpected return value for 0-byte writes, which makes bwrite()
think that the write failed (causing a panic).

This change implements 0-byte writes by not calling into libc at
all, so that we don't have to worry about how to decode the return
value of fwrite().

src/sfstruct.c

index 1dcd427b8836b15aa77861f8fafbe67b4096b67f..73ebe28bfb0b4f9c118404539dc9cb6c7dacce4f 100644 (file)
@@ -172,6 +172,13 @@ bwrite(int fd, const genericptr_t loc, unsigned num)
     int idx = getidx(fd, NOFLG);
 
     if (idx >= 0) {
+        if (num == 0) {
+            /* nothing to do; we need a special case to exit early
+               because glibc fwrite doesn't give reliable
+               success/failure indication when writing 0 bytes */
+            return;
+        }
+
 #ifdef USE_BUFFERING
         if (bw_buffered[idx] && bw_FILE[idx]) {
             failed = (fwrite(loc, (int) num, 1, bw_FILE[idx]) != 1);