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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nmcloughlin at cyclonecommerce dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Fri Feb 14 22:01:29 2025 UTC