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

オープン 初心者向け
#25,817 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

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

評価

難易度
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時間
マージ済み PR(30日)
106

環境構築

はじめの一歩

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

php/php-src のほかの issue

php/php-src の issue をすべて見る

似ている issue

C の issue をもっと見る

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

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