Commit 85566822 authored by Piotr Caban's avatar Piotr Caban Committed by Alexandre Julliard

msvcrt: Reset buffer in fflush on error.

parent 1e833e58
......@@ -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;
}
/*********************************************************************
......
......@@ -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);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment