php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49708 fgetcsv returns NULL instead of FALSE
Submitted: 2009-09-29 11:59 UTC Modified: 2014-01-27 16:52 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: orel at melix dot net Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.3SVN-2009-09-29 (SVN) OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
36 - 28 = ?
Subscribe to this entry?

 
 [2009-09-29 11:59 UTC] orel at melix dot net
Description:
------------
See example of documentation http://www.php.net/fgetcsv

If file doesn't exist there is an endless loop.
fgetcsv returns NULL if handler is invalid.





Reproduce code:
---------------
<?php
$row = 1;
$handle = fopen("test.csv", "r"); /* test.csv should not exist */
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";
    }
}
fclose($handle);
?>


Expected result:
----------------
fgetcsv should returns FALSE

Actual result:
--------------
Endless loop, fgetcsv returns NULL

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-29 12:00 UTC] orel at melix dot net
I agree with steven, doc and example should be updated or function should return FALSE (like fclose, fgets, fwrite ...)


Patch for version 6
Index: ext/standard/file.c
===================================================================
--- ext/standard/file.c	(revision 288932)
+++ ext/standard/file.c	(working copy)
@@ -2166,7 +2166,7 @@
 						&delimiter, &delimiter_len, &delimiter_type,
 						&enclosure, &enclosure_len, &enclosure_type,
 						&escape,    &escape_len,    &escape_type) == FAILURE) {
-		return;
+		RETURN_FALSE;
 	}
 
 	PHP_STREAM_TO_ZVAL(stream, &zstream);


Patch for 5.3

Index: ext/standard/file.c
===================================================================
--- ext/standard/file.c	(revision 288932)
+++ ext/standard/file.c	(working copy)
@@ -2034,7 +2034,7 @@
 			&enclosure_str, &enclosure_str_len,
 			&escape_str, &escape_str_len) == FAILURE
 		) {
-			return;
+			RETURN_FALSE;
 		}
 
 		if (delimiter_str != NULL) {
 [2009-09-29 12:41 UTC] jani@php.net
Invalid parameters cause always (almost) in any function NULL to be returned. This is not a bug.
 [2009-09-29 13:09 UTC] orel at melix dot net
So, documentation of return value needs update.
 [2014-01-27 16:21 UTC] jameshfisher at gmail dot com
jani@php.net is clearly wrong here. The documentation specifies that this method "Returns an indexed array containing the fields read, or FALSE on error." As Orel as shown, the implementation does not satisfy this specification. This is the definition of a bug; therefore this is a bug.

There are two possible fixes here: fix the implementation, or fix the specification. Doing neither is not an option.

Therefore, this bug must be re-opened.
 [2014-01-27 16:52 UTC] salathe@php.net
We do mention "garbage in, garbage out" in the manual:

 > If the parameters given to a function are not what
 > it expects, such as passing an array where a string
 > is expected, the return value of the function is 
 > undefined. In this case it will likely return NULL
 > but this is just a convention, and cannot be relied upon.
 >   -- http://www.php.net/manual/en/functions.internal.php

The topic of including this information on every single function's manual page (or individual, exceptional pages) has been raised previously, to delve into it would be off-topic for this particular bug report. Feel free to bring it up (again) on the documentation mailing list.

P.S. The documentation *is not* a specification, and does not drive implementation (though it has happened on rare occasions).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 10:01:29 2024 UTC