php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34415 seg fault when using scandir
Submitted: 2005-09-07 23:13 UTC Modified: 2005-09-28 05:12 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: worthbob01 at yahoo dot com Assigned:
Status: Not a bug Package: Reproducible crash
PHP Version: 5CVS-2005-09-09 (snap) OS: RHES3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: worthbob01 at yahoo dot com
New email:
PHP Version: OS:

 

 [2005-09-07 23:13 UTC] worthbob01 at yahoo dot com
Description:
------------
recursive scandir causes seg fault.  code example from http://us2.php.net/scandir User Contributed Notes.

Reproduce code:
---------------
<?php
$afiles = scandir_recursive("/tmp");
function scandir_recursive($directory)
{
  $folderContents = array();
  $directory = realpath($directory).DIRECTORY_SEPARATOR;
  foreach (scandir($directory) as $folderItem) {
    if ($folderItem != "." AND $folderItem != "..") {
      if (is_dir($directory.$folderItem.DIRECTORY_SEPARATOR)) {
        $folderContents[$folderItem] = scandir_recursive( $directory.$folderItem."\\");
      }
      else {
        $folderContents[] = $folderItem;
      }
    }
  }
  return $folderContents;
}
?>


Expected result:
----------------
nothing as there is not output in the sample.  just expected it to run without a seg fault.

Actual result:
--------------
[bob@dev design]# date;php sample.php;date
Wed Sep  7 16:11:11 CDT 2005
Segmentation fault
Wed Sep  7 16:11:12 CDT 2005
[bob@dev design]# 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-07 23:43 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-09-10 17:04 UTC] worthbob01 at yahoo dot com
[root@dev design]# date; php --version;  php sample.php ; date
Sat Sep 10 10:02:20 CDT 2005
PHP 5.1.0-dev (cli) (built: Sep  9 2005 12:59:00)
Copyright (c) 1997-2005 The PHP Group
Zend Engine v2.1.0-dev, Copyright (c) 1998-2005 Zend Technologies
Segmentation fault
Sat Sep 10 10:02:21 CDT 2005
 [2005-09-10 23:21 UTC] sniper@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a backtrace to see what is happening behind the scenes. To
find out how to generate a backtrace, please read
http://bugs.php.net/bugs-generating-backtrace.php

Once you have generated a backtrace, please submit it to this bug
report and change the status back to "Open". Thank you for helping
us make PHP better.


 [2005-09-18 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2005-09-27 23:30 UTC] aarondoom at cookiedoom dot com
Your function is stuck in a recursive loop, eventually this will seg fault.

If you wanna see it in action add the following lines to the top of the function.

    static $depth = 0;
    echo $depth++;

To fix the problem remove the . "\\" from the tail of "scandir_recursive($directory.$folderItem."\\");".

Here's a cleaned up function to not display errors and so forth.

function scandir_recursive($directory)
{
    $folderContents = array();
    $directory = realpath($directory) . DIRECTORY_SEPARATOR;
    $folderItems = @scandir($directory);

    if (!is_array($folderItems)) return "No access";

    foreach ($folderItems as $folderItem) {
        if (substr($folderItem, 0, 1) != ".") { // Ignore anything hidden
            if (is_dir($directory . $folderItem . DIRECTORY_SEPARATOR))
                $folderContents[$folderItem] = scandir_recursive($directory . $folderItem);
            else
                $folderContents[] = $folderItem;
        }
    }

    return $folderContents;
}

^_^
 [2005-09-28 05:12 UTC] rasmus@php.net
So not a bug.  Don't write infinitely recursing functions.  Chances are you can avoid a crash in this situation by compiling with memory-limit enabled, but the real fix is to fix your code as suggested.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Dec 01 09:00:01 2025 UTC