fjpanag opened a new issue, #9998:
URL: https://github.com/apache/nuttx/issues/9998

   As I see 
[here](https://pubs.opengroup.org/onlinepubs/7908799/xsh/open.html), this is 
the standard prototype for the `open()` function:
   
   ```c
   int open(const char *path, int oflag, ... );
   ```
   
   According to the standard, the varargs may be parsed as a `mode_t` variable, 
but **only** if `O_CREAT` or `O_TMPFILE` is specified in the file flags.
   
   If neither of the two is specified, then the extra arguments are not needed, 
and are ignored.
   
   *From open group:*
   > O_CREAT
   >     If the file exists, this flag has no effect except as noted under 
O_EXCL below. Otherwise, the file is created; the user ID of the file is set to 
the effective user ID of the process; the group ID of the file is set to  the 
group ID of the file's parent directory or to the effective group ID of the 
process;  and the access permission bits (see 
[<sys/stat.h>](https://pubs.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html))
 of the file mode are set to the value of the third argument taken as type 
mode_t modified as follows: a bitwise-AND is performed on the file-mode bits 
and the corresponding bits in the complement of the process' file mode creation 
mask. Thus, all bits in the file mode whose corresponding bit in the file mode 
creation mask is set are cleared. When bits other than the file permission bits 
are set, the effect is unspecified. The third argument does not affect whether 
the file is open for reading, writing or for both.
   
   *From Linux:*
   > The mode argument specifies the file mode bits to be
   >               applied when a new file is created.  If neither O_CREAT
   >               nor O_TMPFILE is specified in flags, then mode is ignored
   >               (and can thus be specified as 0, or simply omitted).  The
   >               mode argument must be supplied if O_CREAT or O_TMPFILE is
   >               specified in flags; if it is not supplied, some arbitrary
   >               bytes from the stack will be applied as the file mode.
   
   ---
   
   However, this is not the case in NuttX.  
   As I see 
[here](https://github.com/apache/nuttx/blob/master/fs/vfs/fs_open.c#L92), NuttX 
will try to parse `ap` also in the case of `O_WRONLY`.
   
   As far as I understand, even `O_WRONLY` will create the file if it does not 
exist, but I cannot see this referenced in any of the standards.
   
   This is a portability and standards conformance issue.  
   Code ported from other platforms may use `O_WRONLY` without specifing the 
mode. In this case `open()` will read garbage from the stack.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to