www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18667] New: Segfault in std.file.dirEntries

https://issues.dlang.org/show_bug.cgi?id=18667

          Issue ID: 18667
           Summary: Segfault in std.file.dirEntries
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Keywords: bootcamp
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: greensunny12 gmail.com

DScanner sometimes segfaults in dirEntries:

---
0x00007ffff6fa6fb6 in __readdir (dirp=0x0) at ../sysdeps/posix/readdir.c:44
44      ../sysdeps/posix/readdir.c: No such file or directory.


std/file.d:4029

std/file.d:4133

std/file.d:4155

(this=0x7ffff7ee9530, root=...) at dsymbol/src/dsymbol/modulecache.d:133

(this=0x7ffff7ee9530, root=...) at dsymbol/src/dsymbol/modulecache.d:132

(this=0x7ffff7ee9530, root=...) at dsymbol/src/dsymbol/modulecache.d:132

(this=..., paths=...) at dsymbol/src/dsymbol/modulecache.d:136


src/rt/dmain2.d:508

at src/rt/dmain2.d:469

src/rt/dmain2.d:508

at src/rt/dmain2.d:469

mainFunc=0xa56010 <D main>) at src/rt/dmain2.d:528

---

https://github.com/dlang/phobos/pull/6344

I haven't been able to reproduce this locally and segfault is copied from the
failing CircleCi run.

The Dlang side:

https://github.com/dlang/phobos/blob/4c4c37d0b464df05e2f5298d4ddc9b6a9f16bb1b/std/file.d#L4029

for (dirent* fdata; (fdata = readdir(_stack[$-1].h)) != null; ) 

The glibc side:

https://github.molgen.mpg.de/git-mirror/glibc/blob/master/sysdeps/posix/readdir.c

#ifndef __READDIR

...
#endif

__READDIR (DIR *dirp)
{
  DIRENT_TYPE *dp;
  int saved_errno = errno;

#if IS_IN (libc)
  __libc_lock_lock (dirp->lock);
#endif

So it looks like we are passing a null pointer to readdir.
A first start would be to modify the dirEntries implementation to ensure that
no invalid data is passed to readdir by e.g. adding more asserts.

--
Mar 26 2018