[cfe-dev] Fix segfault on sizeof(incomplete type)

Chris Lattner clattner at apple.com
Sat Aug 9 14:36:48 PDT 2008


On Aug 9, 2008, at 2:06 PM, Martin Doucha wrote:

> Hi,
> I've reported a segfault before in ASTRecordLayout::LayoutField().  
> There's a NULL pointer dereference if clang tries to calculate size  
> of struct which contains incomplete type (eg. another struct which  
> is declared but not defined). This patch should fix the problem and  
> let clang abort on the appropriate assert instead.
>
> BTW, some of the asserts in ASTContext::getTypeInfo() could be  
> turned into simple compile errors because C99 forbids using sizeof  
> operator on incomplete types.
>
> When you apply this patch, you can close bug 2400. My problem will  
> be fixed and the problem reported by Mr. Lopes is invalid (size_t is  
> defined in <stddef.h> so its use in typedef on line 1 of the test  
> case is not allowed without #include <stddef.h>).

Fixed, thanks!
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20080804/006896.html

Changing it to isIncompleteArrayType wasn't sufficient to fix the  
whole issue, but the rest was pretty easy.  Thanks,

-Chris

>
>
> Regards,
> Martin Doucha
> Index: lib/AST/ASTContext.cpp
> ===================================================================
> --- lib/AST/ASTContext.cpp	(revision 54121)
> +++ lib/AST/ASTContext.cpp	(working copy)
> @@ -369,8 +369,8 @@
>     if (!FD->getIdentifier())
>       FieldAlign = 1;
>   } else {
> -    if (FD->getType()->isIncompleteType()) {
> -      // This must be a flexible array member; we can't directly
> +    if (FD->getType()->isIncompleteArrayType()) {
> +      // This is a flexible array member; we can't directly
>       // query getTypeInfo about these, so we figure it out here.
>       // Flexible array members don't have any size, but they
>       // have to be aligned appropriately for their element type.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list