|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26772 invalid fgets() return value when invalid resource supplied
Submitted: 2004-01-02 21:30 UTC Modified: 2004-01-15 04:16 UTC
From: giovanni at giacobbi dot net Assigned:
Status: Closed Package: Filesystem function related
PHP Version: 4CVS, 5CVS OS: *
Private report: No CVE-ID:
 [2004-01-02 21:30 UTC] giovanni at giacobbi dot net
See the example code. It should return FALSE according to the manual (and to common good sense).

This is causing valid code like

while (($str = fgets($fd)) !== FALSE) {

to go into infinite loop, which is very bad.
I tried to look at the source code but it looks correct, as it  should fall in file.c:1395, at the exit_failed label.

Reproduce code:

$fd = FALSE;

$retval = @fgets($fd);


Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-02 22:20 UTC] giovanni at giacobbi dot net
ok ok, i got it. Follows a working patch.
Well, the patch is just a hack in my opinion, but.. hey, if there is a "php_stream_from_zval_no_verify" macro it means you already hacked it for something..
Maybe i haven't understood the API structure very well yet, but can php_streams.h access directly zend_fetch_resource() without using a zend_list.h macro?

--- php-4.3.4/ext/standard/file.c.orig	2003-10-10 03:38:01.000000000 +0200
+++ php-4.3.4/ext/standard/file.c	2004-01-03 04:08:04.000000000 +0100
@@ -1358,7 +1358,10 @@
-	php_stream_from_zval(stream, arg1);
+	/* we want our way to handle the error */
+	php_stream_from_zval_no_verify(stream, arg1);
+	if (!stream)
+		goto exit_failed;
 	if (argc == 1) {
 		/* ask streams to give us a buffer of an appropriate size */
 [2004-01-14 09:27 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Tue Dec 01 09:01:42 2015 UTC