php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #8893 Missing last character from server response.
Submitted: 2001-01-24 21:19 UTC Modified: 2001-01-25 05:51 UTC
From: nmcloughlin at cyclonecommerce dot com Assigned:
Status: Closed Package: cURL related
PHP Version: 4.0.4pl1 OS: Redhat 6.2 (Linux)
Private report: No CVE-ID: None
 [2001-01-24 21:19 UTC] nmcloughlin at cyclonecommerce dot com
#!/usr/local/bin/php -q
<?php
$co = curl_init();
curl_setopt($co, CURLOPT_URL, $url);
curl_setopt($co, CURLOPT_RETURNTRANSFER, 1);
$str = curl_exec($co);
print "[END]";
print "\nReturned : ".$str."[END]\n";
?>

If $url is a page that prints out 0123456789 (no breaks and no \n).  The following will be output :

[END]
Returned : 012345678[END]

Now if you turn CURLOPT_RETURNTRANSFER off (set to 0), you will get the following :

0123456789[END]
Returned : [END]

The returns are mutually exclusive (expected).  However, in the first example, the 9 is missing.

Upon further examination, I found the following to be at fault :

ext/curl/curl.c line 653 :
ref_data[stat_sb.st_size - 1] = '\0';

which should be
ref_data[stat_sb.st_size] = '\0';

It's a classic off-by-one error with a while loop.  No need to offset in this case (pos is one less than intended).

while ((b = fread(buf, 1, sizeof(buf), fp)) > 0) {
 memcpy(ret_data + pos, buf, b);
 pos += b;
}
 //ret_data[stat_sb.st_size - 1] = '\0';  //clips off last char
ret_data[stat_sb.st_size] = '\0';  //works


--------------- MY CONFIG --------------
Running as a CGI
--without-mysql --enable-ftp --with-oci8 --with-sybase-ct
Kernel 2.2.14-5.0 SMP i686 
Redhat 6.2
----------------------------------------

-Neal McLoughlin

P.S. - I came across this bug while integrating cURL functions with XML-RPC for PHP (www.xmlrpc.com).  The xmlrpc library uses a standard socket.  That's fine until you need to make a https (SSL) request.  The response from the server is an xml body.  I was unable to validate any of the responses from the server.  I was missing the final > character to close my xml doc properly. This one drove me crazy for about 2 days.  Until, of course, I noticed the missing >.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-01-25 05:51 UTC] sniper@php.net
this is fixed in CVS already. Try latest CVS snapshot
from http://snaps.php.net/

--Jani
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Feb 24 03:01:25 2020 UTC