msvcrt: Change how fread splits reading into chunks.

It affects fd stream position and buffer content on unsuccessul reads.

Signed-off-by: Piotr Caban <piotr@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Piotr Caban 2017-11-30 19:55:16 +01:00 committed by Alexandre Julliard
parent b1ccf61f22
commit 1899eadf3e
2 changed files with 29 additions and 5 deletions

View File

@ -4300,7 +4300,7 @@ MSVCRT_size_t CDECL MSVCRT__fread_nolock(void *ptr, MSVCRT_size_t size, MSVCRT_s
while(rcnt>0)
{
int i;
if (!file->_cnt && rcnt<MSVCRT_BUFSIZ && (file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
if (!file->_cnt && rcnt<file->_bufsiz && (file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF))) {
i = MSVCRT__read(file->_file, file->_base, file->_bufsiz);
file->_ptr = file->_base;
if (i != -1) {
@ -4319,10 +4319,10 @@ MSVCRT_size_t CDECL MSVCRT__fread_nolock(void *ptr, MSVCRT_size_t size, MSVCRT_s
}
} else if (rcnt > INT_MAX) {
i = MSVCRT__read(file->_file, ptr, INT_MAX);
} else if (rcnt < MSVCRT_BUFSIZ) {
} else if (rcnt < (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ)) {
i = MSVCRT__read(file->_file, ptr, rcnt);
} else {
i = MSVCRT__read(file->_file, ptr, rcnt - MSVCRT_BUFSIZ/2);
i = MSVCRT__read(file->_file, ptr, rcnt - rcnt % (file->_bufsiz ? file->_bufsiz : MSVCRT_INTERNAL_BUFSIZ));
}
pread += i;
rcnt -= i;

View File

@ -222,7 +222,7 @@ static void test_readmode( BOOL ascii_mode )
static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9\r\n\r\nA,B,C,D,E\r\nX,Y,Z";
static const char padbuffer[] = "ghjghjghjghj";
static const char nlbuffer[] = "\r\n";
char buffer[2*BUFSIZ+256];
static char buffer[8192];
const char *optr;
int fd;
FILE *file;
@ -327,7 +327,7 @@ static void test_readmode( BOOL ascii_mode )
ok(feof(file)==0,"feof failure in %s\n", IOMODE);
ok(fread(buffer,2,1,file)==0,"fread failure in %s\n",IOMODE);
ok(feof(file)!=0,"feof failure in %s\n", IOMODE);
/* test some additional functions */
rewind(file);
ok(ftell(file) == 0,"Did not start at beginning of file in %s\n", IOMODE);
@ -350,6 +350,30 @@ static void test_readmode( BOOL ascii_mode )
fclose (file);
unlink ("fdopen.tst");
/* test INTERNAL_BUFSIZ read containing 0x1a character (^Z) */
fd = open("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
ok(fd != -1, "open failed\n");
memset(buffer, 'a', sizeof(buffer));
buffer[1] = 0x1a;
ok(write(fd, buffer, sizeof(buffer)) == sizeof(buffer), "write failed\n");
ok(close(fd) != -1, "close failed\n");
fd = open("fdopen.tst", O_RDONLY);
ok(fd != -1, "open failed\n");
file = fdopen(fd, ascii_mode ? "r" : "rb");
ok(file != NULL, "fdopen failed\n");
memset(buffer, 0, sizeof(buffer));
i = fread(buffer, 4096, 1, file);
ok(!i, "fread succeeded\n");
ok(file->_bufsiz == 4096, "file->_bufsiz = %d\n", file->_bufsiz);
for(i=0; i<4096; i++)
if(buffer[i] != (i==1 ? 0x1a : 'a')) break;
ok(i==4096, "buffer[%d] = %d\n", i, buffer[i]);
fclose(file);
unlink("fdopen.tst");
}
static void test_asciimode(void)