bug in PHP API, with fix attached

Tim Starling t.starling@physics.unimelb.edu.au
Thu, 04 Dec 2003 10:16:17 +1100

This is a multi-part message in MIME format.
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

The PHP API _load_items() function reads in blocks of 1024 bytes. By
coincidence, I was testing with a message that caused the "\r\nEND\r\n"
terminator to fall across a 1024 byte boundary. Usually this is no big
deal, but in my case, the part in the first block was "\r\nEND" and in the
second block it was just "\r\n". Any empty line is skipped by a continue
statement without any further processing, leading to another call to
socket_read() and hence causing the client to block indefinitely.

The patch is to check if it is near the end, and to read slightly more
than 1024 bytes if it is.

Tim Starling
Wikipedia http://en.wikipedia.org/

Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;

Index: MemCachedClient.inc.php
RCS file: /cvsroot/wikipedia/phase3/includes/MemCachedClient.inc.php,v
retrieving revision 1.3
diff -r1.3 MemCachedClient.inc.php
> 			// if we're almost at the end, read the rest, so 
> 			// that we don't corrupt the \r\nEND\r\n
> 			if ($bytes_read >= $len)
> 			{
> 				$lastbit = socket_read($sock, $len - $bytes_read + 7, PHP_BINARY_READ);
> 				$line .= $lastbit;
> 				$buf .= $lastbit;
> 				$bytes_read += strlen($lastbit);
> 			}
< ?>
\ No newline at end of file
> ?>