From 53471ce33bf15d7981092bdfb35bd75f03efaf03 Mon Sep 17 00:00:00 2001 From: Alastair Houghton Date: Mon, 13 May 2024 14:18:17 +0100 Subject: [PATCH] [Windows] Fix a problem with opening files read only. `_O_RDONLY` is zero, so we can't test for it by AND-ing. --- Sources/System/Internals/WindowsSyscallAdapters.swift | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Sources/System/Internals/WindowsSyscallAdapters.swift b/Sources/System/Internals/WindowsSyscallAdapters.swift index 3e3f9c6..1e35ee9 100644 --- a/Sources/System/Internals/WindowsSyscallAdapters.swift +++ b/Sources/System/Internals/WindowsSyscallAdapters.swift @@ -621,14 +621,15 @@ fileprivate struct DecodedOpenFlags { } dwDesiredAccess = 0 - if (oflag & _O_RDONLY) != 0 { + switch (oflag & (_O_RDONLY|_O_WRONLY|_O_RDWR)) { + case _O_RDONLY: dwDesiredAccess |= DWORD(GENERIC_READ) - } - if (oflag & _O_WRONLY) != 0 { + case _O_WRONLY: dwDesiredAccess |= DWORD(GENERIC_WRITE) - } - if (oflag & _O_RDWR) != 0 { + case _O_RDWR: dwDesiredAccess |= DWORD(GENERIC_READ) | DWORD(GENERIC_WRITE) + default: + break } bInheritHandle = WindowsBool((oflag & _O_NOINHERIT) == 0)