Description:
------------
I am using the following library
https://github.com/ideaconnect/idct-sftp-client
When I try to get a directory listing of files from an SFTP connection I am getting the following error.
Segmentation fault: 11
I am using PHP 5.6.9 that was installed via Homebrew. However I also tried using the PHP version that shipped with Zend Server (uninstalled after testing) which I believe was 5.6.x and I was getting the same results.
Test script:
---------------
<?php
use \IDCT\Networking\Ssh\SftpClient;
$sftp = new SftpClient();
$credentials = Credentials::withPassword('username', 'password');
$sftp->setCredentials($credentials);
$sftp->connect('hostname.goes.here', 22);
$remote_files = $sftp->getFileList('your_path_here/');
Expected result:
----------------
I expect an array of filenames that reside on the server path that I specify in the getFileList method.
Actual result:
--------------
Segmentation Fault: 11
This is the line that causes the actual segmentation fault which I verified with var_dump before the loop, inside the loop and after the loop. The only place the var_dump worked was before the loop.
https://github.com/ideaconnect/idct-sftp-client/blob/master/src/SftpClient.php#L521

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports

[2015-07-02 18:49 UTC] info at josephcrawford dot com

I should also note that everything else is working properly. I can connect, upload, download files without any issues. It is solely when I tried to list the directory contents that the segmentation fault is thrown.

[2015-07-02 18:58 UTC] info at josephcrawford dot com

Also here is a directory listing showing the permissions for the folder in which I am trying to get a directory listing from.
dr-xr--r-- 1 user group 0 Jul 2 12:00 Images

[2015-07-02 19:42 UTC] info at josephcrawford dot com

Since I was using the ssh2 extension that was installed via homebrew I was led through the process of using pecl to reinstall the ssh2 extension. I used the following command.
sudo pecl install ssh2
When I tested my application I received the same segmentation fault as using the homebrew installed version of the extension.
jcrawford@Josephs-Mac-mini:~/TraderTools (dev *) # ./traders.php --action=import
Execution started at 3:26 PM
Segmentation fault: 11

[2015-07-02 19:58 UTC] info at josephcrawford dot com

I believe this is an issue with the ssh2_lib and the report may need to be moved from Stream Related to SSH2 Lib.

You can help progress on this bug report by making a minimal test case using only the ssh2 functions and not using a library. Also, upgrading libssh2 to the latest version will improve your chances.
The ssh2 extension is only a really thin wrapper over the libssh2 library. It might well be that libssh2 is at fault in this case.
It might also be relevant to state make and version of the ssh server you are talking to.

[2015-07-02 20:39 UTC] info at josephcrawford dot com

I wouldn't have the first clue on how to upgrade libssh2 on Mac OSX as for the make/version of the ssh software I am not sure. I do not have ssh access only sftp access.

[2015-07-02 20:41 UTC] info at josephcrawford dot com

Also homebrew doesn't have a version > 1.5.0 so to get the latest I am sure that would require compiling and I haven't a clue where all this stuff is stored (paths, etc) on mac osx as I know it's not usually the same as on linux.

[2015-07-02 21:29 UTC] info at josephcrawford dot com

Does PHP's implementation of readdir care how many files are in the directory? I was able to get this code to work perfectly fine with a directory of about 5 files. However the directory I am trying to get a directory listing for has around 60,000 image files in the directory and it's causing the segmentation fault.

[2015-07-02 22:09 UTC] info at josephcrawford dot com

I am also going to add that I have tried to use the following work-arounds and they also fail when it comes time to read the directory.
<?php
// Segmentation Fault: 11
$files = scandir("ssh2.sftp://$sftp/$remotePath");
// This works
$handle = dir("ssh2.sftp://$sftp/$remotePath");
// UNTIL you do this
while (false !== ($entry = $handle->read())) {
It would appear that it is something in the lower level C programming for the PHP core or the ssh2 extension which would segmentation fault when it comes to memory issues.
I verified the total number of files in the directory is 29,516 and not 60,000 like I first estimated. My resized images folder has 60,000 images in it but I am not trying to list that directory.

[2015-07-02 22:54 UTC] info at josephcrawford dot com

I also upgraded my libssh2 to 1.6.0 and received the same segmentation fault results.
I am thinking that the problem is with the ssh2_ extension as if this was in the core it would have been encountered by now. Since the ssh2 extension is used far less than the php core stream related code.

Here is some output of what I get when I use the base directory that only has a few files in the directory. Again the images folder has 29,516 images and the directory structure and server management are not under my control.
jcrawford@Josephs-Mac-mini:~/TraderTools (dev *%) # php sftp_test.php
array(4) {
[0]=>
string(14) "CatInfoJSC.zip"
[1]=>
string(16) "ItemAvailJSC.zip"
[2]=>
string(15) "METADataJSC.txt"
[3]=>
string(6) "Images"
}