diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index aaa8d71b..d7151b58 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -1061,11 +1061,12 @@ class Net_SFTP extends Net_SSH2 $dirs = explode('/', preg_replace('#^/|/(?=/)|/$#', '', $path)); $temp = &$this->stat_cache; - foreach ($dirs as $dir) { + $max = count($dirs) - 1; + foreach ($dirs as $i=>$dir) { if (!isset($temp[$dir])) { $temp[$dir] = array(); } - if ($dir == end($dirs)) { + if ($i === $max) { $temp[$dir] = $value; break; } @@ -1085,8 +1086,9 @@ class Net_SFTP extends Net_SSH2 $dirs = explode('/', preg_replace('#^/|/(?=/)|/$#', '', $path)); $temp = &$this->stat_cache; - foreach ($dirs as $dir) { - if ($dir == end($dirs)) { + $max = count($dirs) - 1; + foreach ($dirs as $i=>$dir) { + if ($i === $max) { unset($temp[$dir]); return true; } @@ -2206,7 +2208,7 @@ class Net_SFTP extends Net_SSH2 $result = $this->_query_stat_cache($path); if (isset($result)) { - // return true if $result is an array or if it's int(1) + // return true if $result is an array or if it's an stdClass object return $result !== false; } } diff --git a/tests/Functional/Net/SFTPUserStoryTest.php b/tests/Functional/Net/SFTPUserStoryTest.php index bdf8e7e3..4a2656ed 100644 --- a/tests/Functional/Net/SFTPUserStoryTest.php +++ b/tests/Functional/Net/SFTPUserStoryTest.php @@ -342,7 +342,23 @@ class Functional_Net_SFTPUserStoryTest extends PhpseclibFunctionalTestCase } /** - * @depends testSortOrder + * on older versions this would result in a fatal error + * @depends testReadlink + * @group github402 + */ + public function testStatcacheFix($sftp) + { + $sftp->mkdir('testdir'); + $sftp->chdir('testdir'); + $sftp->touch('testdir'); + $sftp->chdir('..'); + $sftp->delete('testdir', true); + + return $sftp; + } + + /** + * @depends testStatcacheFix */ public function testChDirUpHome($sftp) {