From 67926ec8c344db4250f1e74a5943d0b8a536e950 Mon Sep 17 00:00:00 2001 From: Piotr Caban Date: Tue, 28 Nov 2017 19:48:33 +0100 Subject: [PATCH] msvcrt: Set errno when close() is called on already closed fd. Based on a patch by Olly Betts. Signed-off-by: Piotr Caban Signed-off-by: Alexandre Julliard --- dlls/msvcrt/file.c | 2 +- dlls/msvcrt/tests/file.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c index 5eaee2b67b7..1e662e9067e 100644 --- a/dlls/msvcrt/file.c +++ b/dlls/msvcrt/file.c @@ -1051,7 +1051,7 @@ int CDECL MSVCRT__close(int fd) if (fd == MSVCRT_NO_CONSOLE_FD) { *MSVCRT__errno() = MSVCRT_EBADF; ret = -1; - } else if (!(info->wxflag & WX_OPEN)) { + } else if (!MSVCRT_CHECK_PMT_ERR(info->wxflag & WX_OPEN, MSVCRT_EBADF)) { 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 99ceb97b88b..00f5e7e8719 100644 --- a/dlls/msvcrt/tests/file.c +++ b/dlls/msvcrt/tests/file.c @@ -2466,6 +2466,12 @@ static void test_close(void) ok(!GetHandleInformation(h, &flags), "GetHandleInformation succeeded\n"); ok(close(fd2), "close(fd2) succeeded\n"); + /* test close on already closed fd */ + errno = 0xdeadbeef; + ret1 = close(fd1); + ok(ret1 == -1, "close(fd1) succeeded\n"); + ok(errno == 9, "errno = %d\n", errno); + /* test close on stdout and stderr that use the same handle */ h = CreateFileA("fdopen.tst", GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);