SplFileObject::isReadable and SplFileObject::isWriteable provide an untrustworthy interface
未关闭
还没有人认领这个 Issue。
Extension: spl
Feature
Status: Verified
- 主要语言
- C
- 星标
- 40.4k
- 派生
- 8.2k
- 平均合并
- 2 天 13 小时
- 30 天内合并 PR
- 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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
入口点是 SplFileObject::isReadable()、SplFileObject::isWriteable() 以及示例中演示的读写方法。首先跟踪这些方法如何评估文件系统权限与资源模式。为 r+、只写和追加情况添加回归测试覆盖,然后验证报告的输出和预期输出。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, php
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 42/100