php / php/php-src

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

Offen
#9,833 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Extension: spl Feature Status: Verified
Vorherrschende Sprache
C
Sterne
40.4k
Forks
8.1k
Ø Merge
2 T. 13 Std.
Gemergte PRs (30 T.)
96

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Die Einstiegspunkte sind SplFileObject::isReadable(), SplFileObject::isWriteable() und die im Beispiel demonstrierten Lese-/Schreibmethoden. Beginne damit nachzuverfolgen, wie diese Methoden Dateisystemberechtigungen im Vergleich zum Ressourcenmodus auswerten. Füge eine Regressionstestabdeckung für die Fälle r+, Nur-Schreiben und Anhängen hinzu und überprüfe anschließend die gemeldeten und erwarteten Ausgaben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c, php
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.