php.net |  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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
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
Description:
------------
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:
---------------
Example:

<?php

// create sftp ressource $sftp

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

while (!feof($h)) {
    echo(fread($h,10));
}
?>

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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-06 07:10 UTC] daniel dot mueller at inexio dot net
Here is a link to the patch:
http://merkur.inexio.net/patches/pecl-bug-17142.diff

(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:
---------------
#!/usr/bin/php
<?php
$connection = ssh2_connect('my.hostname.com', 22);
ssh2_auth_password($connection, 'mylogin', 'mypassword');
$sftp = ssh2_sftp($connection);
$stream = fopen('ssh2.sftp://'.$sftp.'/home/mylogin/test', 'w');
fwrite($stream, "bob");
fclose($stream);

print_r(file_get_contents('ssh2.sftp://'.$sftp.'/home/mylogin/test'));
//Works fine !

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

Expected result:
----------------
bob*bob*

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] bjori@php.net
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 pecl.php.net.

In case this was a pecl.php.net 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-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 22:01:33 2024 UTC