diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c index 2516570db7e..422e95deff9 100644 --- a/dlls/msvcrt/file.c +++ b/dlls/msvcrt/file.c @@ -1047,7 +1047,11 @@ int CDECL MSVCRT__close(int fd) int ret; TRACE(":fd (%d) handle (%p)\n", fd, info->handle); - if (!(info->wxflag & WX_OPEN)) { + + if (fd == MSVCRT_NO_CONSOLE_FD) { + *MSVCRT__errno() = MSVCRT_EBADF; + ret = -1; + } else if (!(info->wxflag & WX_OPEN)) { ret = -1; } else if (fd == MSVCRT_STDOUT_FILENO && info->handle == get_ioinfo_nolock(MSVCRT_STDERR_FILENO)->handle) { diff --git a/dlls/msvcrt/tests/file.c b/dlls/msvcrt/tests/file.c index e0b94cf7f34..543eb3ff08d 100644 --- a/dlls/msvcrt/tests/file.c +++ b/dlls/msvcrt/tests/file.c @@ -1527,6 +1527,7 @@ static void test_invalid_stdin_child( void ) { HANDLE handle; ioinfo *info; + int ret; errno = 0xdeadbeef; handle = (HANDLE)_get_osfhandle(STDIN_FILENO); @@ -1538,6 +1539,21 @@ static void test_invalid_stdin_child( void ) ok(info->wxflag == 0xc1, "info->wxflag = %x\n", info->wxflag); ok(stdin->_file == -2, "stdin->_file = %d\n", stdin->_file); + + errno = 0xdeadbeef; + ret = fclose(stdin); + ok(ret == -1, "fclose(stdin) returned %d\n", ret); + ok(errno == EBADF, "errno = %d\n", errno); + + errno = 0xdeadbeef; + ret = close(-2); + ok(ret == -1, "close(-2) returned %d\n", ret); + ok(errno == EBADF, "errno = %d\n", errno); + + errno = 0xdeadbeef; + ret = close(STDIN_FILENO); + ok(ret==-1 || !ret, "close(STDIN_FILENO) returned %d\n", ret); + ok((ret==-1 && errno==EBADF) || (!ret && errno==0xdeadbeef), "errno = %d\n", errno); } static void test_invalid_stdin( const char* selfname )