php / php/php-src

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

オープン
#9,833 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Extension: spl Feature Status: Verified
主要言語
C
スター
40.4k
フォーク
8.1k
平均マージ
2日 13時間
マージ済み PR(30日)
96

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

エントリーポイントは SplFileObject::isReadable()、SplFileObject::isWriteable()、および例で示されている読み取り/書き込みメソッドです。まず、これらのメソッドがファイルシステムの権限とリソースモードをどのように評価しているかを追跡します。r+、書き込み専用、追加のケースに対するリグレッションカバレッジを追加し、報告された出力と期待される出力を検証します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, php
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。