Search code examples
carrayssegmentation-faulttraversalstrlen

Segmentation fault in strlen when using FTS fts_open()


I have a bug that I've found boils down to this:

#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>
#include <stdlib.h>

int main () {
    char *LOG_ROOT = "/var/log";

    FTS *ftsp; 
    FTSENT *p, *chp;
    int fts_options = FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR;

    char *paths[] = { LOG_ROOT };
    fts_open(paths, fts_options, NULL);
}

Why does this segfault?


Solution

  • The first arg. is expected to be a NULL terminated array of character pointers.

    char *paths[] = { LOG_ROOT, NULL};