|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-07-02 18:45 UTC] info at josephcrawford dot com
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 Patchesssh2_getpath (last revision 2019-03-20 20:01 UTC by ken at pardiac dot com)ssh2_findpath (last revision 2019-03-20 19:48 UTC by ken at pardiac dot om) ssh2_sftp_pathfix (last revision 2015-11-06 17:10 UTC by ken at pardiac dot com) Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 09:00:01 2025 UTC |
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.This is about as simple as you can get. The following code provides me with the Segmentation Fault as seen below.. // RESPONSE jcrawford@Josephs-Mac-mini:~/TraderTools (dev *%) # php sftp_test.php Segmentation fault: 11 // CODE <?php $ssh2 = ssh2_connect(‘you.domain.here’, 8022); ssh2_auth_password($ssh2, ‘username’, ‘password’); $sftp = ssh2_sftp($ssh2); $handle = opendir("ssh2.sftp://$sftp/YourDirHere/”); $files = array(); while (false != ($entry = readdir($handle))){ $files[] = $entry; } var_dump($files);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" }This bug was introduced as a by-product of bug # 59794. I have re-written the fix for that bug in the pecl ssh2-0.12 package source. Here is my patch to the ssh2_fopen_wrappers.c module. This fix re-constructs the filename from the path, query and fragment components after the URL is parsed. The problem was that if the path component matched any previous part of the URL string which is the case when just a "/" is the path. It matches the first "/" in the URL. Chaos ensues. Perhaps one of the developers can take a look at this. Ken Pardiac --- ssh2_fopen_wrappers.c 2012-10-02 16:18:07.000000000 -0400 +++ ssh2_fopen_wrappers.c 2015-11-06 11:20:40.436490279 -0500 @@ -219,12 +219,29 @@ } /* - Find resource->path in the path string, then copy the entire string from the original path. - This includes ?query#fragment in the path string + Put path, query and fragment from the url back together to get a filename + that may have contained ? or #. */ - s = resource->path; - resource->path = estrdup(strstr(path, resource->path)); - efree(s); + if ( resource->query || resource->fragment ) { + char *p; + int len = strlen(resource->path) + 1; + if ( resource->query ) + len += strlen(resource->query) + 1; + if ( resource->fragment ) + len += strlen(resource->fragment) + 1; + p = emalloc( len ); + strcpy( p, resource->path ); + if ( resource->query ) { + strcat( p, "?" ); + strcat( p, resource->query ); + } + if ( resource->fragment ) { + strcat( p, "#" ); + strcat( p, resource->fragment ); + } + efree( resource->path ); + resource->path = p; + } /* Look for a resource ID to reuse a session */ s = resource->host;I had the same issue after update my php. Try to use intval() function: $handle = opendir("ssh2.sftp://".intval($sftp)."/path/to/directory");I will try to explain this bug further since my patch from 2015 has not been incorporated into the source. Here is a line of code from ssh2_fopen_wrappers.c that causes this bug: (comment included) /* Find resource->path in the path string, then copy the entire string from the original path. This includes ?query#fragment in the path string */ resource->path = estrdup(strstr(path, resource->path)); This line of code -- and therefore this bug -- was introduced as a fix for bug #59794. That line of code is attempting to get a string containing the part, query and fragment from the path variable. Consider this value for the path variable: ssh2.sftp://Resource id #5/topdir?a=something#heading1 When resource->path is "/topdir", the result is that "/topdir?a=something#heading1" gets assigned to resource->path. The intent is that resource->path gets a string which includes the query and fragment part of the url. Now consider the case when resource->path is "/". After the line of code is executed, resource->path becomes "//Resource id#5/topdir#heading1". This is clearly not what you want. A better solution is to build up the path by using resource->path, resource->query and resource->fragment. (The resource struct is returned by php_url_parse) That is what the patch I posted years ago does. It *fixes* the bug. It does not just provide a workaround. You may also need to apply the patch for bug #73597 which removes "Resource id #" from the path string before calling php_url_parse().