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

Richard Smith richard at metafoo.co.uk
Wed Sep 26 12:49:38 PDT 2012


Can you try adding

  printf ("%p %zd\\n", d, _Alignof(*d));

before the line which fails? Is the pointer correctly aligned?

On Wed, Sep 26, 2012 at 12:38 PM, Sean McBride <sean at rogue-research.com>wrote:

> On Wed, 26 Sep 2012 10:40:47 -0400, Joe Ranieri said:
>
> >> 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);
> >> }
> >>
> >
> > *SNIP*
> >
> >Just a hunch: is d->d_name an unaligned pointer? We ran into a similar
> >issue with gethostbyname.
>
> Joe,
>
> Thanks for your reply.  But I'm afraid I don't follow.  Unaligned with
> respect to what?  The 'struct dirent' is declared by the system basically
> like so:
>
> #pragma pack()
> struct dirent {
>         uint64_t  d_ino;
>         uint64_t  d_seekoff;
>         uint16_t  d_reclen;
>         uint16_t  d_namlen;
>         uint8_t   d_type;
>         char      d_name[1024];
> }
>
> Note the 'pragma pack'; is that the alignment you're referring to?  On my
> system, I don't see any pragma pack with gethostbyname()'s 'struct
> hostent', what problem did you have with it?
>
> I just tried something else: it's not just the d_name field, but accessing
> any field triggers the SIGILL.
>
> Cheers,
>
> --
> ____________________________________________________________
> Sean McBride, B. Eng                 sean at rogue-research.com
> Rogue Research                        www.rogue-research.com
> Mac Software Developer              Montréal, Québec, Canada
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120926/7bc4518f/attachment.html>


More information about the cfe-dev mailing list