Skip to content

Possible false positive with -Wcast-align and #pragma pack(push, 2) #9591

@seanm

Description

@seanm
Bugzilla Link 9219
Version trunk
OS MacOS X
CC @rjmccall

Extended Description

Consider this C code:

#include <CoreServices/CoreServices.h>

int main(void)
{
	FSCatalogInfo catalogInfo = {};
	FileInfo* fileInfo = (FileInfo*)(&catalogInfo.finderInfo);
	(void)fileInfo;
	
	return 0;
}

With -Wcast-align clang says:

test.c:6:23: warning: cast from 'UInt8 (*)[16]' to 'FileInfo *' (aka
'struct FileInfo *')
      increases required alignment from 1 to 2 [-Wcast-align]
        FileInfo* fileInfo = (FileInfo*)(&catalogInfo.finderInfo);
                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I think this is maybe a false positive, though I'm not sure.

a) The docs for the FSCatalogInfo struct say:

/*

  • kFSCatInfoFinderInfo / file type, creator, flags, location.
  • Coerce to a File/FolderInfo to use.
    */
    UInt8 finderInfo[16];

Implying that the cast should be safe.

b) Files.h starts with #pragma pack(push, 2). I'm thinking maybe clang does not take that into account.

If clang is indeed correct here, then I'll file a Radar against CarbonCore/Files.h.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillaclang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerincompleteIssue not complete (e.g. missing a reproducer, build arguments, etc.)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions