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

Sean McBride sean at rogue-research.com
Wed Sep 26 12:38:27 PDT 2012

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.


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.


Sean McBride, B. Eng                 sean at rogue-research.com
Rogue Research                        www.rogue-research.com 
Mac Software Developer              Montréal, Québec, Canada

More information about the cfe-dev mailing list