Email List: Xaustin-review-lX
[All Lists]

Defect in XSH fopen

To: austin-review-l@xxxxxxxxxxxxx
Subject: Defect in XSH fopen
From: Geoff Clare <gwc@xxxxxxxxxxxxx>
Date: Thu, 3 May 2007 12:30:11 +0100
@ page 398 line 12892 section fopen objection [gwc fopen fd flags]

Problem:

Defect code :  2. Omission

The description of fopen() does not state any requirements for the
flags set on the open file description.  For example, it does not
require that O_NONBLOCK is clear, or that the O_APPEND flag is set
when the stream is opened in append mode.  (It requires that in append
mode writes to the file are forced to the then current end-of-file,
which is what O_APPEND does, but this could be implemented using some
other internal mechanism instead of setting O_APPEND.)

Not specifying the access mode is a less serious omission, but it does
in theory allow Weirdnix implementations where the underlying fd is
always opened with O_RDWR (file permissions permitting) despite the
stream itself only being readable (for "r") or writable (for "w" or "a").

The simplest way to fix the omission would be to require that the
underlying fd is allocated and opened as if by a call to open() with
specific flags.  This would automatically bring in some other
requirements that are currently explicitly stated; the redundant text
can be removed.

The freopen description needs to be changed to match.

Action:

On page 398 line 12892 delete "The fopen() function shall allocate a
file descriptor as open() does."

On page 399 line 12897 change:

    The largest value that can be represented correctly in an object
    of type off_t shall be established as the offset maximum in the
    open file description.

to:

    The file descriptor associated with the opened stream shall be
    allocated and opened as if by a call to open() with the following
    flags:

       fopen() mode      open() flags

       r or rb           O_RDONLY
       w or wb           O_WRONLY|O_CREAT|O_TRUNC
       a or ab           O_WRONLY|O_CREAT|O_APPEND
       r+ or rb+ or r+b  O_RDWR
       w+ or wb+ or w+b  O_RDWR|O_CREAT|O_TRUNC
       a+ or ab+ or a+b  O_RDWR|O_CREAT|O_APPEND

(with the CX shading retained).

On page 439 line 14449 section freopen change:

    The largest value that can be represented correctly in an object
    of type off_t shall be established as the offset maximum in the
    open file description.

to:

    If filename is not a null pointer, or if filename is a null pointer
    and the specified mode change necessitates the file descriptor 
    associated with the stream to be closed and reopened, the file
    descriptor associated with the reopened stream shall be allocated
    and opened as if by a call to open() with the following flags:

       freopen() mode    open() flags

       r or rb           O_RDONLY
       w or wb           O_WRONLY|O_CREAT|O_TRUNC
       a or ab           O_WRONLY|O_CREAT|O_APPEND
       r+ or rb+ or r+b  O_RDWR
       w+ or wb+ or w+b  O_RDWR|O_CREAT|O_TRUNC
       a+ or ab+ or a+b  O_RDWR|O_CREAT|O_APPEND

(with the CX shading retained).

[Note to the editor: in the drafts of the next revision there is some
additional text on the freopen page that should also be removed.  In
draft 2 it is page 452 line 14996: "The freopen() function shall
allocate a file descriptor in the same way as open()."]

<Prev in Thread] Current Thread [Next in Thread>