bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: openat wrapper assumes that O_RDONLY is disjoint from R_RDWR


From: Bruno Haible
Subject: Re: openat wrapper assumes that O_RDONLY is disjoint from R_RDWR
Date: Sat, 07 Mar 2020 18:36:09 +0100
User-agent: KMail/5.1.3 (Linux/4.4.0-174-generic; KDE/5.18.0; x86_64; ; )

Dan Gohman wrote:
> That would fix the problem for systems that define O_RDONLY as Hurd does,
> while not breaking any known systems.
> 
> It wouldn't be correct on a hypothetical system which defines the constants
> like this:
> 
> #define O_WRONLY 0
> #define O_RDONLY 1
> #define O_RDWR 2
> 
> though I don't know of any platforms which do this.

Reviewing the include files of the various platforms shows that
such a hypothetical system doesn't exist:

                     O_RDONLY   O_WRONLY   O_RDWR

AIX
Android
BeOS
Cygwin
FreeBSD
glibc/Linux
Haiku
HP-UX
Interix
IRIX
macOS
mingw, MSVC
Minix
MirBSD
musl
NetBSD
OpenBSD
OSF/1
pips
Plan 9
Solaris
                         0          1         2

glibc/Hurd               1          2         3


So this patch should be safe:


2020-03-07  Bruno Haible  <address@hidden>

        openat: Fix theoretically possible issue on GNU/Hurd.
        Reported by Dan Gohman <address@hidden> in
        <https://lists.gnu.org/archive/html/bug-gnulib/2020-03/msg00000.html>.
        * lib/openat.c (rpl_openat): When testing whether flags contains O_RDWR,
        ignore the bits that are also set in O_RDONLY.

diff --git a/lib/openat.c b/lib/openat.c
index d2c84e8..7d67dc4 100644
--- a/lib/openat.c
+++ b/lib/openat.c
@@ -100,7 +100,7 @@ rpl_openat (int dfd, char const *filename, int flags, ...)
          directories,
        - if O_WRONLY or O_RDWR is specified, open() must fail because the
          file does not contain a '.' directory.  */
-  if (flags & (O_CREAT | O_WRONLY | O_RDWR))
+  if (flags & (O_CREAT | O_WRONLY | (O_RDWR & ~O_RDONLY)))
     {
       size_t len = strlen (filename);
       if (len > 0 && filename[len - 1] == '/')




reply via email to

[Prev in Thread] Current Thread [Next in Thread]