msvcrt: Reset buffer in fflush on error.

Signed-off-by: Piotr Caban <piotr@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
feature/deterministic
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 */
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;
}
/*********************************************************************

View File

@ -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);
}