From:             strideroflands at yahoo dot com
Operating system: Windows
PHP version:      5.4.16
Package:          Streams related
Bug Type:         Bug
Bug description:fgets() fails to return newline at end of string

Description:
------------
---
>From manual page: http://www.php.net/function.fgets
---

In "C" programming, the function fgets() returns the end of line character
at the end of the string.  The description of stream_get_line says

"This function is nearly identical to fgets() except in that it allows end
of line delimiters other than the standard \n, \r, and \r\n, and does not
return the delimiter itself."

This suggests that fgets() should indeed return a newline character.  Such
functionality would be very useful.  In "C", fgets(fp) is known to be a
known source of bugs, since it overwrites memory buffers when the line is
longer than intended.  In PHP, fgets() allows an additional parameter to
limit the size of a buffer it can fill.  If fgets() does not return a
newline if it reaches one, how are we supposed to know if it reached the
end of the "length" parameter, or if it reached a newline?

In the test script, it can report if a line is longer than expected only if
the newline is returned.  However, this does not happen.  The only way to
know if this happened would be 1) write your own fgets() function, 2) see
if you are at the end of the file, use ftell to determine the position,
read a byte, seek to the position ftell just reported, and test on the
byte.  This is more laborious than the simple test for a newline below. 
The lack of returning the newline effectively obviates the usefulness of
the "length" parameter.  If one were to encounter a large video file, e.g.,
the whole thing might be read into memory, and there would be no convenient
way to detect it.

I generally detest any changes that break existing code by changing the
language.  Therefore, I suggest making another function that won't be
giving existing applications newline characters they didn't have before; or
else having a variable that you set to make fgets() return newlines like it
should.

This is actually vers. 5.4.15.

Test script:
---------------
$hndl = fopen("aFileWithNewlines.txt", "r+");
$line= fgets ($hndl,80);
if (strpos($line,"\n")==false)
    echo "error";
else
    echo "no error";


Expected result:
----------------
"error" if the line is longer than 80 characters, otherwise, "no error".

Actual result:
--------------
always "error"

-- 
Edit bug report at https://bugs.php.net/bug.php?id=65116&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=65116&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=65116&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=65116&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=65116&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=65116&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=65116&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=65116&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=65116&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=65116&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=65116&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=65116&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=65116&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=65116&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65116&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=65116&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=65116&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=65116&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=65116&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=65116&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=65116&r=mysqlcfg

Reply via email to