streams/userspace.c: PHP_STREAM_FLAG_NO_FCLOSE guard missing in stream_truncate handler (incomplete fix for GH-25586)

未关闭 适合新手
#25,817 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
74/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c, php
领域
backend, testing

调研方向

从 main/streams/userspace.c 中的 php_stream_truncate_userspace 开始,将其回调处理方式与为 GH-25586 添加的保护逻辑进行比较。使用 issue 中的 PHP stream-wrapper 示例复现,并检查在 stream_truncate 期间 fclose() 会发出警告,同时执行能够完成。确认现有的 stream 测试覆盖了此行为,然后为此情况添加或更新回归测试。

由索引模型根据 Issue 内容生成。

描述

Bug Status: Needs Triage
Description

Same pattern as GH-25586: the stream_truncate handler in
main/streams/userspace.c (~line 980) calls the user callback without
PHP_STREAM_FLAG_NO_FCLOSE, allowing fclose() inside the callback to
succeed and free the stream while C code may still reference it.

The following code:

<?php
class TruncStream {
    public static $res;

    public function stream_open($path, $mode, $opts, &$opened): bool {
        return true;
    }
    public function stream_truncate(int $newSize): bool {
        fclose(self::$res);  // succeeds; no NO_FCLOSE guard
        return true;
    }
    public function stream_read(int $count): string { return ''; }
    public function stream_eof(): bool { return true; }
    public function stream_stat(): array|false { return false; }
    public function stream_write(string $data): int { return strlen($data); }
}

stream_wrapper_register('x', 'TruncStream');
$fp = fopen('x://test', 'w');
TruncStream::$res = $fp;
ftruncate($fp, 0);  // triggers stream_truncate → fclose() completes silently
echo "done\n";

Resulted in this output:

done

But I expected this output instead:

Warning: Cannot close stream with active callback in ...
done

Suggested fix (main/streams/userspace.c, php_stream_truncate_userspace):

bool originally_no_fclose = stream->flags & PHP_STREAM_FLAG_NO_FCLOSE;
stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
call_result = zend_call_method_with_1_params(...); /* stream_truncate */
if (!originally_no_fclose) {
    stream->flags ^= PHP_STREAM_FLAG_NO_FCLOSE;
}
PHP Version
PHP 8.6.0 (cli) (built: Nov 19 2026 15:17:27) (ZTS)
Operating System

Ubuntu 26.04

主要语言
C
星标
41.9k
派生
8.5k
平均合并
2 天 18 小时
30 天内合并 PR
106

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

php/php-src 的其他 Issue

查看 php/php-src 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。