[php-src] Issue #8555: Giving a closed stream resource to the fwrite function causes a TypeError

From: Date: Sat, 14 May 2022 14:21:52 +0000
Subject: [php-src] Issue #8555: Giving a closed stream resource to the fwrite function causes a TypeError
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-241515@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/8555 Author: CMDR-Vulkarius ### Description The following code: ```php <?php $res = fopen("php://memory", "w+"); fclose($res); var_dump(@fwrite($res, "\n")); ``` Resulted in this output: ``` Fatal error: Uncaught TypeError: fwrite(): supplied resource is not a valid stream resource in /tmp/bug-fwrite.php:4 Stack trace: #0 /tmp/bug-fwrite.php(4): fwrite(Resource id #5, '\n') #1 {main} thrown in /tmp/bug-fwrite.php on line 4 ``` But I expected this output instead: ``` bool(false) ``` This closed-stream test works as expected on PHP 7.2.34 and 7.4.29. No such change to the behaviour of the fwrite function has been announced or documented since PHP 7. If a little context helps, I have a library ported to PHP in which a write error should throw a specific exception, where the PHP implementation checks for fwrite returning false and generates the exception. The associated unit test passes with PHP 7, and the same test discovered that TypeError very quickly after I downloaded and compiled PHP 8.1.6. All three of those PHP versions involved behave as expected when the call to fclose is removed, all printing "int(1)". ### PHP Version PHP 8.1.6 ### Operating System Linux Mint 20.1

« previous php.bugs (#241515) next »