[cfe-dev] Miscompilation of sizeof

Reid Kleckner reid.kleckner at gmail.com
Mon Dec 12 08:29:09 PST 2011


Most people use clang with assertions disabled, so this doesn't really
solve the underlying problem.

Reid

On Sun, Dec 11, 2011 at 3:55 AM, Abramo Bagnara <abramo.bagnara at gmail.com>wrote:

> Il 10/12/2011 22:41, David Blaikie ha scritto:
> > Did you mean to attach a patch showing the assert you intend to add?
>
> I believed it was not needed, but I've attached it now for review.
>
> With that patch applied the following testcase (specific for 64 bit
> architecture) now triggers the assertion instead to be horribly
> miscompiled.
>
> #include <stdio.h>
>
> typedef int x[1UL<<59];
>
> int main() {
>  printf("%lu\n", sizeof(x));
> }
>
>
> > On Sat, Dec 10, 2011 at 12:52 PM, Abramo Bagnara
> > <abramo.bagnara at gmail.com> wrote:
> >>
> >> Ping.
> >>
> >>>
> >>> In ASTContext:::getTypeInfo(const Type *T) const we have:
> >>>
> >>>   uint64_t Width=0;
> >>>
> >>>   case Type::ConstantArray: {
> >>>     const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
> >>>
> >>>     std::pair<uint64_t, unsigned> EltInfo =
> >>> getTypeInfo(CAT->getElementType());
> >>>     Width = EltInfo.first*CAT->getSize().getZExtValue();
> >>>
> >>> But this multiplication can overflow (because for reasons that I don't
> >>> known getTypeInfo return width specified in bits).
> >>>
> >>> If there are no objections I'd add an assert: probably we'll induce
> some
> >>> crashes, but I believe that this would be *far* better than to
> >>> miscompile the code (and assertions will be triggered only when code
> >>> would be miscompiled).
> >>>
>
> _______________________________________________
> 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/20111212/ae0fbf48/attachment.html>


More information about the cfe-dev mailing list