Piotr Caban : msvcrt: Reset buffer in fflush on error.

Alexandre Julliard julliard at winehq.org
Fri Jun 12 16:01:17 CDT 2020


Module: wine
Branch: master
Commit: 855668221d37f00122537ff36258cd78876839bc
URL:    https://source.winehq.org/git/wine.git/?a=commit;h=855668221d37f00122537ff36258cd78876839bc

Author: Piotr Caban <piotr at codeweavers.com>
Date:   Fri Jun 12 12:43:00 2020 +0200

msvcrt: Reset buffer in fflush on error.

Signed-off-by: Piotr Caban <piotr at codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 dlls/msvcrt/file.c       | 11 ++++++-----
 dlls/msvcrt/tests/file.c | 18 +++++++++++++++++-
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c
index 825d442bfa..6b7471a56a 100644
--- a/dlls/msvcrt/file.c
+++ b/dlls/msvcrt/file.c
@@ -656,21 +656,22 @@ void msvcrt_init_io(void)
 /* INTERNAL: Flush stdio file buffer */
 static int msvcrt_flush_buffer(MSVCRT_FILE* file)
 {
+    int ret = 0;
+
     if((file->_flag & (MSVCRT__IOREAD|MSVCRT__IOWRT)) == MSVCRT__IOWRT &&
             file->_flag & (MSVCRT__IOMYBUF|MSVCRT__USERBUF)) {
         int cnt=file->_ptr-file->_base;
         if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
             file->_flag |= MSVCRT__IOERR;
-            return MSVCRT_EOF;
-        }
-
-        if(file->_flag & MSVCRT__IORW)
+            ret = MSVCRT_EOF;
+        } else if(file->_flag & MSVCRT__IORW) {
             file->_flag &= ~MSVCRT__IOWRT;
+        }
     }
 
     file->_ptr=file->_base;
     file->_cnt=0;
-    return 0;
+    return ret;
 }
 
 /*********************************************************************
diff --git a/dlls/msvcrt/tests/file.c b/dlls/msvcrt/tests/file.c
index 61f6ffe373..fc6dacd71f 100644
--- a/dlls/msvcrt/tests/file.c
+++ b/dlls/msvcrt/tests/file.c
@@ -687,7 +687,7 @@ static void test_fflush( void )
   char buf1[16], buf2[24];
   char *tempf;
   FILE *tempfh;
-  int ret;
+  int ret, fd;
 
   tempf=_tempnam(".","wne");
 
@@ -728,7 +728,23 @@ static void test_fflush( void )
   ok(memcmp(buf1, buf2, sizeof(buf1)) == 0, "Got unexpected data (%c)\n", buf2[0]);
 
   fclose(tempfh);
+  unlink(tempf);
+
+  /* test flush failure */
+  tempfh = fopen(tempf,"wb");
+  ok(tempfh != NULL, "Can't open test file.\n");
+  fwrite(obuf, 1, sizeof(obuf), tempfh);
+  fd = tempfh->_file;
+  tempfh->_file = -1;
+
+  ok(tempfh->_ptr - tempfh->_base, "buffer is empty\n");
+  ret = fflush(tempfh);
+  ok(ret == EOF, "expected EOF, got %d\n", ret);
+  ok(!(tempfh->_ptr - tempfh->_base), "buffer should be empty\n");
+  ok(!tempfh->_cnt, "tempfh->_cnt = %d\n", tempfh->_cnt);
 
+  tempfh->_file = fd;
+  fclose(tempfh);
   unlink(tempf);
   free(tempf);
 }




More information about the wine-cvs mailing list