#87 [Ver->Ctl]: readLine() method has problems with "\n" line endings

From: Date: Sat, 18 Oct 2003 11:03:27 +0000
Subject: #87 [Ver->Ctl]: readLine() method has problems with "\n" line endings
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-22789@lists.php.net to get a copy of this message
ID: 87 Updated by: heino@php.net Reported By: avb@php.net -Status: Verified +Status: Critical Bug Type: Net_Socket Operating System: Any PHP Version: Irrelevant New Comment: I guess not being able to send empty lines (terminated by \n only) is rather critical. This diff seems to work; people, what say you? -------------------------------- --- Socket.php.orig Sat Oct 18 12:13:19 2003 +++ Socket.php Sat Oct 18 12:48:23 2003 @@ -392,12 +392,25 @@ $line = ''; $timeout = time() + $this->timeout; while (!$this->eof() && (!$this->timeout || time() < $timeout)) { - $line .= $this->gets($this->lineLength); - if (strlen($line) >= 2 && - (substr($line, -2) == "\r\n" || - substr($line, -1) == "\n")) { - return rtrim($line); - } + $data = $this->gets($this->lineLength); + + if ($data === false) { + // Handle error... + } + + $line .= $data; + + if (substr($line, -1) != "\n") { + continue; + + } else { + if (substr($line, -2) == "\r\n") { + return substr($line, 0, -2); + } + if (substr($line, -1) == "\n") { + return substr($line, 0, -1); + } + } } return $line; } -------------------------------- Previous Comments: ------------------------------------------------------------------------ [2003-10-18 06:57:48] neufeind@php.net How about this code (lines 396-400) in Net_Socket if (strlen($line) >= 1 && (substr($line, -1) == "\n")) { return rtrim($line); } Should be enough, right? ------------------------------------------------------------------------ [2003-10-11 09:06:27] avb@php.net Description: ------------ This code (lines 396-400) in Net_Socket if (strlen($line) >= 2 && (substr($line, -2) == "\r\n" || substr($line, -1) == "\n")) { return rtrim($line); } prevent the method from correctly returning an empty string if a line containing just "\n" is read from socket. Reproduce code: --------------- sock-bug.php: <?php require_once 'Net/Socket.php'; $sock =& new Net_Socket(); // To be able to test with local file instead of real socket... $sock->fp = fopen('sock-bug.txt', 'rb'); $i = 1; while (!$sock->eof()) { $line = $sock->readLine(); echo $i++ . ':' . $line . "\n"; } // while ?> sock-bug.txt (should have \n line endings!): foo bar baz Expected result: ---------------- 1:foo 2: 3:bar 4: 5: 6:baz Actual result: -------------- 1:foo 2: bar 3: 4:baz ------------------------------------------------------------------------ -- Edit this bug report at http://pear.php.net/bugs/bug.php?id=87&edit=1

« previous php.pear.dev (#22789) next »