|  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
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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: orel at melix dot net
New email:
PHP Version: OS:


 [2009-09-29 11:59 UTC] orel at melix dot net
See example of documentation

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

Reproduce code:
$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";
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";

Expected result:
fgetcsv should returns FALSE

Actual result:
Endless loop, fgetcsv returns NULL


Add a Patch

Pull Requests

Add a Pull Request


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;
 	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;
 		if (delimiter_str != NULL) {
 [2009-09-29 12:41 UTC]
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 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]
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.
 >   --

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-2021 The PHP Group
All rights reserved.
Last updated: Sun Jan 24 00:01:23 2021 UTC