msvcrt: Reset buffer in fflush on error.

Signed-off-by: Piotr Caban <piotr@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Piotr Caban 2020-06-12 12:43:00 +02:00 committed by Alexandre Julliard
parent 1e833e58de
commit 855668221d
2 changed files with 23 additions and 6 deletions

View File

@ -656,21 +656,22 @@ void msvcrt_init_io(void)
/* INTERNAL: Flush stdio file buffer */ /* INTERNAL: Flush stdio file buffer */
static int msvcrt_flush_buffer(MSVCRT_FILE* file) static int msvcrt_flush_buffer(MSVCRT_FILE* file)
{ {
int ret = 0;
if((file->_flag & (MSVCRT__IOREAD|MSVCRT__IOWRT)) == MSVCRT__IOWRT && if((file->_flag & (MSVCRT__IOREAD|MSVCRT__IOWRT)) == MSVCRT__IOWRT &&
file->_flag & (MSVCRT__IOMYBUF|MSVCRT__USERBUF)) { file->_flag & (MSVCRT__IOMYBUF|MSVCRT__USERBUF)) {
int cnt=file->_ptr-file->_base; int cnt=file->_ptr-file->_base;
if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) { if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
file->_flag |= MSVCRT__IOERR; file->_flag |= MSVCRT__IOERR;
return MSVCRT_EOF; ret = MSVCRT_EOF;
} } else if(file->_flag & MSVCRT__IORW) {
if(file->_flag & MSVCRT__IORW)
file->_flag &= ~MSVCRT__IOWRT; file->_flag &= ~MSVCRT__IOWRT;
} }
}
file->_ptr=file->_base; file->_ptr=file->_base;
file->_cnt=0; file->_cnt=0;
return 0; return ret;
} }
/********************************************************************* /*********************************************************************

View File

@ -687,7 +687,7 @@ static void test_fflush( void )
char buf1[16], buf2[24]; char buf1[16], buf2[24];
char *tempf; char *tempf;
FILE *tempfh; FILE *tempfh;
int ret; int ret, fd;
tempf=_tempnam(".","wne"); 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]); ok(memcmp(buf1, buf2, sizeof(buf1)) == 0, "Got unexpected data (%c)\n", buf2[0]);
fclose(tempfh); 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); unlink(tempf);
free(tempf); free(tempf);
} }