php / php/php-src

SplFileObject::isReadable and SplFileObject::isWriteable provide an untrustworthy interface

Open
#9,833 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Extension: spl Feature Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

isReadable and isWriteable only consider the file system permissions but that isn't actually reflective of the state of the object because read and write methods also consider the resource mode so if you trust them you can can run into unexpected bugs as demonstrated in the example below.

Specifically
1 . Streams don't look readable?
2. Resources that are opened write only look readable.

The following code:

<?php

function showFile(\SplFileObject $file) {
    var_dump($file->isReadable());
    if ($file->isReadable()) {
      var_dump($file->fread($file->getSize()));
    }
    echo str_repeat('=', 80) . PHP_EOL;
}

showFile(new SplFileObject(__FILE__));
$f = new SplFileObject('php://temp', 'r+');
$f->fwrite('content');
showFile($f);
showFile(new SplFileObject(__FILE__, 'a'));

Resulted in this output:

bool(true)
string(376) "<?php

function showFile(\SplFileObject $file) {
    var_dump($file->isReadable());
    if ($file->isReadable()) {
      var_dump($file->fread($file->getSize()));
    }
    echo str_repeat('=', 80) . PHP_EOL;
}

showFile(new SplFileObject(__FILE__));
$f = new SplFileObject('php://temp', 'r+');
$f->fwrite('content');
showFile($f);
showFile(new SplFileObject(__FILE__, 'a'));
"
================================================================================
bool(false)
================================================================================
bool(true)
PHP Notice:  SplFileObject::fread(): Read of 8192 bytes failed with errno=9 Bad file descriptor in /tmp/tmp.php on line 6
bool(false)
================================================================================

But I expected this output instead:

bool(true)
string(376) "<?php

function showFile(\SplFileObject $file) {
    var_dump($file->isReadable());
    if ($file->isReadable()) {
      var_dump($file->fread($file->getSize()));
    }
    echo str_repeat('=', 80) . PHP_EOL;
}

showFile(new SplFileObject(__FILE__));
$f = new SplFileObject('php://temp', 'r+');
$f->fwrite('content');
showFile($f);
showFile(new SplFileObject(__FILE__, 'a'));
"
================================================================================
bool(true)
string(7) "content"
================================================================================
bool(false)
PHP Version

all

Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The entry points are SplFileObject::isReadable(), SplFileObject::isWriteable(), and the read/write methods demonstrated in the example. Start by tracing how these methods evaluate filesystem permissions versus the resource mode. Add regression coverage for the r+, write-only, and append cases, then verify the reported and expected outputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.