Bug #49708 [Nab]: fgetcsv returns NULL instead of FALSE
| From: | salathe@php.net | Date: | Mon, 27 Jan 2014 16:52:28 +0000 |
| Subject: | Bug #49708 [Nab]: fgetcsv returns NULL instead of FALSE | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-184035@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=49708&edit=1
ID: 49708
Updated by: salathe@php.net
Reported by: orel at melix dot net
Summary: fgetcsv returns NULL instead of FALSE
Status: Not a bug
Type: Bug
Package: Filesystem function related
Operating System: All
PHP Version: 5.3SVN-2009-09-29 (SVN)
Block user comment: N
Private report: N
New Comment:
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).
Previous Comments:
------------------------------------------------------------------------
[2014-01-27 16:21:15] 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.
------------------------------------------------------------------------
[2009-09-29 13:09:11] orel at melix dot net
So, documentation of return value needs update.
------------------------------------------------------------------------
[2009-09-29 12:41:19] jani@php.net
Invalid parameters cause always (almost) in any function NULL to be returned. This is not a bug.
------------------------------------------------------------------------
[2009-09-29 12:00:16] 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 11:59:52] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=49708&edit=1