Shanks0224 opened a new pull request, #22241:
URL: https://github.com/apache/nuttx/pull/22241

   fflush() of a stream that was only opened for reading returned EBADF and 
left the file offset where the read-ahead had put it, so after
   
     fread(buf, 1, 3, f);
     fflush(f);
   
   lseek(fileno(f), 0, SEEK_CUR) returned the size of the buffered block 
instead of 3.  An update stream holding read data was not changed either.
   
   POSIX says that fflush() of a seekable input stream sets the file offset to 
the stream position and discards the characters pushed back with ungetc().  Do 
that in fflush() and fflush_unlocked() only, and only when the file can seek.  
The read data of a pipe or a terminal is kept, because it could not be read 
again, and lib_fflush_unlocked(), which the read path calls before every read, 
is not changed, so reading an "r+" stream does not discard its buffer.  
fflush() of a read stream now returns 0 instead of EBADF.
   
   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## Summary
     
     POSIX says that `fflush()` of a seekable input stream sets the file offset 
to
     the stream position and discards characters pushed back with `ungetc()`.
     NuttX returned EBADF and left the offset where the read-ahead put it. Do 
this
     in `fflush()` and `fflush_unlocked()` only, and only when the file can 
seek.
     Pipes and terminals keep their read data, and the internal flush that the 
read
     path calls before every read is unchanged. Split from #22179.
     
     ## Impact
     
     `fflush()` of a read stream now returns 0 instead of EBADF. No impact on 
the
     build system, documentation or hardware.
     
     ## Testing
     
     Build host: Linux x86_64. Every case below gives the same result as glibc 
on
     the host:
     
     - read stream, `fread` 3 bytes, `fflush()`: returns 0, offset 3;
     - "r+" stream with read data, `fflush()`: offset at the stream position;
     - "r+" file read with 12 `fgetc()` calls: correct data, same speed as 
master;
     - FIFO opened "r+", two `fgetc()`: a, b;
     - FIFO opened "r", `fgetc()`, `fflush(f)`, `fgetc()`: no data lost;
     - stdin on a FIFO, `getchar()`, `fflush(NULL)`, `getchar()`: x, y.
     
     Known difference: after `ungetc()` and `fflush()`, the next byte is the one
     after the pushed-back position (POSIX discards the pushback); glibc returns
     the byte one further on.


-- 
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