|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #59138 Missing EOF detection in ssh2.sftp:// streams
Submitted: 2010-03-30 09:07 UTC Modified: 2011-09-21 13:28 UTC
From: daniel dot mueller at inexio dot net Assigned: bjori (profile)
Status: Closed Package: ssh2 (PECL)
PHP Version: 5.3.1 OS: FreeBSD 7.2-RELEASE
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: daniel dot mueller at inexio dot net
New email:
PHP Version: OS:


 [2010-03-30 09:07 UTC] daniel dot mueller at inexio dot net
php_ssh2_sftp_stream_read() doesn't check for the End OF File condition.
So, feof() on such streams always returns false.

Also negative return values from libssh2_sftp_read aren't handled, correctly.

I have attached a patch which fixes these problems in php_ssh2_sftp_stream_read (and php_ssh2_sftp_stream_write, which has similar problems)

Reproduce code:


// create sftp ressource $sftp

$h = fopen("ssh2.sftp://$sftp/tmp/testfile", 'r');

while (!feof($h)) {

Here is the patch:
Index: ssh2_sftp.c
--- ssh2_sftp.c	(Revision 297176)
+++ ssh2_sftp.c	(Arbeitskopie)
@@ -107,8 +107,11 @@
 static size_t php_ssh2_sftp_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
 	php_ssh2_sftp_handle_data *data = (php_ssh2_sftp_handle_data*)stream->abstract;
+	ssize_t bytes_written;
-	return libssh2_sftp_write(data->handle, buf, count);
+	bytes_written = libssh2_sftp_write(data->handle, buf, count);
+	return (size_t)(bytes_written<0 ? 0 : bytes_written);
 /* }}} */
@@ -117,8 +120,13 @@
 static size_t php_ssh2_sftp_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
 	php_ssh2_sftp_handle_data *data = (php_ssh2_sftp_handle_data*)stream->abstract;
+	ssize_t bytes_read;
-	return libssh2_sftp_read(data->handle, buf, count);
+	bytes_read = libssh2_sftp_read(data->handle, buf, count);
+	stream->eof = (bytes_read <= 0 && bytes_read != LIBSSH2_ERROR_EAGAIN);
+	return (size_t)(bytes_read<0 ? 0 : bytes_read);
 /* }}} */

Expected result:
The example outputs the file and ends.

Actual result:
Without the patch, the loop runs forever.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-06 07:10 UTC] daniel dot mueller at inexio dot net
Here is a link to the patch:

(The code in the bug report got wrapped)
 [2010-12-08 11:32 UTC] sjaillet at gmail dot com
Same here !

OS: Ubuntu 10.4 TLS
PHP version : 5.3.2-1ubuntu4.5

Test script:
$connection = ssh2_connect('', 22);
ssh2_auth_password($connection, 'mylogin', 'mypassword');
$sftp = ssh2_sftp($connection);
$stream = fopen('ssh2.sftp://'.$sftp.'/home/mylogin/test', 'w');
fwrite($stream, "bob");

//Works fine !

$source_handle = fopen('ssh2.sftp://'.$sftp.'/home/mylogin/test', "r");
       while (!feof($source_handle)) { //Infinite loop
               $buffer = fread($source_handle,1024);
               echo "*".$buffer."*";

Expected result:

Actual result:
bob*bob*********************************************... and so on.
 [2011-06-29 11:01 UTC] davidjmemmett at gmail dot com
Is this issue likely to be acknowledged by the developers any 
time soon? It was submitted over a year ago, a fix is 
supplied and the library is still broken today.

Reproducible using PHP 5.3.5/libssh2-php 0.11.2-1

An ugly workaround for now is to check the accumulative size 
of the file you're reading against the total file size.
 [2011-09-21 13:28 UTC]
This bug has been fixed in SVN.

In case this was a documentation problem, the fix will show up at the
end of next Sunday (CET) on

In case this was a website problem, the change will show
up on the website in short time.
Thank you for the report, and for helping us make PECL better.

PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Dec 02 22:01:23 2020 UTC