php / php/php-src

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

Đang mở
#9,833 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Extension: spl Feature Status: Verified
Ngôn ngữ chính
C
Star
40.4k
Fork
8.2k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Các điểm đầu vào là SplFileObject::isReadable(), SplFileObject::isWriteable() và các phương thức đọc/ghi được minh họa trong ví dụ. Trước tiên, hãy theo dõi cách các phương thức này đánh giá quyền của hệ thống tệp so với chế độ tài nguyên. Thêm phạm vi kiểm thử hồi quy cho các trường hợp r+, chỉ ghi và nối thêm, sau đó xác minh các đầu ra được báo cáo và mong đợi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
c, php
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
42/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.