[cfe-dev] -fcatch-undefined-behavior false positive with readdir()?

Joe Ranieri joe at alacatialabs.com
Wed Sep 26 07:40:47 PDT 2012


On Wed, Sep 26, 2012 at 10:29 AM, Sean McBride <sean at rogue-research.com> wrote:
> Hi all,
>
> The following C code works without -fcatch-undefined-behavior, and worked with it too, until a few weeks ago when it was given new smarts:
>
> -------------------
> #include <dirent.h>
> #include <stdio.h>
>
> int main (void)
> {
>         DIR* dir = opendir("/usr/lib");
>         if (dir)
>         {
>                 struct dirent* d = NULL;
>                 while ((d = readdir(dir)) != NULL)
>                 {
>                         const char* local = d->d_name; // bam!
>                         printf ("%s\n", local);
>                 }
>                 closedir(dir);
>         }
>
>         return 0;
> }
> -------------------
>
> $ clang --version
> clang version 3.2 (trunk 164470)
>
> $ clang -Weverything -fcatch-undefined-behavior test.c
> (no output)
>
> $ ./a.out
> .
> ..
> arc
> Illegal instruction: 4
>
> Several colleagues have reviewed the code, and so I'm guess I'm asking if I should file a bug, or have missed something subtle here.

Just a hunch: is d->d_name an unaligned pointer? We ran into a similar
issue with gethostbyname.

-- Joe Ranieri



More information about the cfe-dev mailing list