msvcrt: Fix freopen() on FILE with invalid underlying fd.

Signed-off-by: Eric Pouech <epouech@codeweavers.com>
This commit is contained in:
Eric Pouech 2023-08-17 10:16:37 +02:00 committed by Alexandre Julliard
parent 65b6e237cd
commit 626f8d75e8
2 changed files with 20 additions and 3 deletions

View file

@ -4588,9 +4588,7 @@ FILE* CDECL _wfreopen(const wchar_t *path, const wchar_t *mode, FILE* file)
TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
LOCK_FILES();
if (!file || ((fd = file->_file) < 0))
file = NULL;
else
if (file)
{
fclose(file);
if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)

View file

@ -1236,6 +1236,25 @@ static void test_freopen( void )
ok(ret == EOF, "fclose(file) succeeded\n");
ok(errno == 0xdeadbeef, "errno is %d\n", errno);
file = fopen(filename1, "rb");
ok(file != NULL, "couldn't open %s\n", filename1);
close(file->_file);
file->_file = -1;
new = freopen(filename2, "rb", file);
ok(new == file, "freopen() didn't return same FILE*\n");
fd = fileno(file);
ok(fd > 0, "fileno() returned %d\n", fd);
ch = '#';
ret = fread(&ch, 1, 1, file);
ok(ret == 1, "fread() returned %d\n", ret);
ok(ch == '2', "Unexpected char\n");
ret = fclose(file);
ok(ret == 0, "fclose(file) returned %d\n", ret);
unlink(filename1);
unlink(filename2);
}