|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 15:00:02 2025 UTC |
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.