php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #76995 fseek() breaks reading in read/append (a+) mode
Submitted: 2018-10-10 14:58 UTC Modified: 2018-10-10 16:08 UTC
From: miloslav dot hula at gmail dot com Assigned:
Status: Open Package: Streams related
PHP Version: 7.3.0RC2 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: miloslav dot hula at gmail dot com
New email:
PHP Version: OS:

 

 [2018-10-10 14:58 UTC] miloslav dot hula at gmail dot com
Description:
------------
URL in a test script shows, how fseek() in 'a+' mode breaks contents reading.

Documentation of fopen() says, that fseek() is OK, but documentation of fseek() says, that seeking in a+ mode is bad idea.

In documentation of fopen():

a+ - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended.

In documentation of fseek():

If you have opened the file in append (a or a+) mode, any data you write to the file will always be appended, regardless of the file position, and the result of calling fseek() will be undefined.

Test script:
---------------
https://3v4l.org/gTXsR


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-10-10 15:03 UTC] spam2 at rhsoft dot net
but what is the bug?

> seeking in a+ mode is bad idea
> and the result of calling fseek() will be undefined

undefined is what it is - undfined - therw is no right/wrong for undefined
 [2018-10-10 15:07 UTC] miloslav dot hula at gmail dot com
At least, documentation is misleading. "a+ mode is for reading/append". How can one read from file, when cannot seek?

Could be clearer with: "only appended data can be read, seeking not supported"
 [2018-10-10 15:11 UTC] miloslav dot hula at gmail dot com
Or: "In this mode, fseek() only affects the reading position". Sound like positive claim.
 [2018-10-10 16:07 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-10-10 16:07 UTC] requinix@php.net
Unfortunately this really is one of those "undefined behavior" situations, and PHP isn't the one responsible for it.

Look at the fopen(3) docs on Linux: https://linux.die.net/man/3/fopen
> Reads and writes may be intermixed on read/write streams in any order. Note
> that ANSI C requires that a file positioning function intervene between output
> and input, unless an input operation encounters end-of-file. (If this condition
> is not met, then a read is allowed to return the result of writes other than
> the most recent.) Therefore it is good practice (and indeed sometimes necessary
> under Linux) to put an fseek(3) or fgetpos(3) operation between write and read
> operations on such a stream. This operation may be an apparent no-op (as in
> fseek(..., 0L, SEEK_CUR) called for its synchronizing side effect.
 [2018-10-10 16:08 UTC] requinix@php.net
-Status: Not a bug +Status: Open -Type: Bug +Type: Documentation Problem
 [2018-10-10 16:08 UTC] requinix@php.net
And seconds after I hit Submit, I realize that documenting append mode's fseek quirks would be a good idea.
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Mon Dec 10 09:01:25 2018 UTC