Subject: lib/14340: scandir aborts with Segmentation fault (core dumped)
To: None <gnats-bugs@gnats.netbsd.org>
From: None <cwlh@nortelnetworks.com>
List: netbsd-bugs
Date: 10/24/2001 14:23:35
>Number:         14340
>Category:       lib
>Synopsis:       scandir aborts with Segmentation fault (core dumped)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Oct 24 14:24:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Chris Hobbs
>Release:        1.5.1
>Organization:
Nortel Networks
>Environment:
NetBSD kanNetBsd1 1.5.1 NetBSD 1.5.1 (GENERIC) #56: Mon Jul  2 15:54:23 CEST 2001     he@nsa.uninett.no:/usr/src/sys/arch/i386/compile/GENERIC i386
>Description:
Calls to scandir when the directory does not exist correctly return a
value of -1. Calls to scandir when the directory does exist cause a 
segmentation fault within scandir.
>How-To-Repeat:
Compile and run this program which was written to isolate the problem.


#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/param.h>
#include <stddef.h>

int main();

int main()
    {
    int     n;
    char    cwd[MAXPATHLEN+1];
    struct dirent ***namelist;

    // get our current working directory
    // to use as an example directory

    getcwd(cwd,MAXPATHLEN);
    printf("cwd = <%s>\n",cwd);

    // then go to get the contents
    // of that directory

    printf("Starting scandir\n");
    n = scandir(cwd,namelist,NULL,NULL);
    printf("Back from scandir\n");

    printf("There were %d entries\n",n);

    return 0;
    }

>Fix:
Don't use scandir: use opendir/readdir/closedir instead.
>Release-Note:
>Audit-Trail:
>Unformatted: