http://llvm.org/bugs/show_bug.cgi?id=16055

            Bug ID: 16055
           Summary: malloc type check needs to consider subclass struct
                    pattern
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Static Analyzer
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected]
    Classification: Unclassified

Created attachment 10536
  --> http://llvm.org/bugs/attachment.cgi?id=10536&action=edit
test case

While not super common common today, there exists a pattern in C where you
create subclasses by having a struct with another struct as the first member
and casting between the two.

The incompatible pointer types check for malloc needs to take this into
consideration. Casting to the type that's the first member of the sizeof type
should be valid, see attached sample code (which should analyze clean).

E.g. PHP zend headers still uses this as of version 5.3.3
(ALLOCATE_PERMANENT_ZVAL macro). Anyone who programmed in classic MacOS should
also be familiar with it. :)

Steps to reproduce:

cc --analyze foo.c

Expected result:

Empty foo.plist

Actual result:

foo.c:15:18: warning: Result of 'malloc' is converted to a pointer of type
'struct a', which is incompatible with sizeof operand
      type 'struct b'
        struct a *var = malloc(sizeof (struct b));
        ~~~~~~~~~~      ^~~~~~ ~~~~~~~~~~~~~~~~~
1 warning generated.

Version info:
clang version 3.4 (http://llvm.org/git/clang.git
7b773002d0beae96a8a34ec9413877242381ec75) (http://llvm.org/git/llvm.git
d6b4caf291aa8c3cd4bcb5f3b55b72621b506278)
Target: x86_64-unknown-linux-gnu
Thread model: posix

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
LLVMbugs mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/llvmbugs

Reply via email to