[PATCH] Fix for PR20110: Don't assume that the subexpression of a bit cast has pointer type if the bit cast has pointer type

Richard Smith richard at metafoo.co.uk
Fri Jun 27 09:04:47 PDT 2014


On Tue, Jun 24, 2014 at 3:13 PM, Richard Trieu <rtrieu at google.com> wrote:

> When a bit cast expression has pointer type, don't assume that the
> subexpression also has pointer type.  It is possible that the subexpression
> is a value type instead.  This patch checks the type of the subexpression
> and calls the proper function.  Prevents the assertion failure reported in
> PR20110.
>
> http://llvm.org/bugs/show_bug.cgi?id=20110
>
> http://reviews.llvm.org/D4280
>
> Files:
>   lib/Sema/SemaChecking.cpp
>   test/SemaCXX/PR20110.cpp
>
> Index: test/SemaCXX/PR20110.cpp
> ===================================================================
> --- test/SemaCXX/PR20110.cpp
> +++ test/SemaCXX/PR20110.cpp
> @@ -0,0 +1,13 @@
> +// RUN: %clang_cc1 -fsyntax-only -verify %s
> +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
> +// expected-no-diagnostics
> +
> +template <char const *p>
> +class A {
> +  char const *get_p() { return *p; }
> +};
> +template <int p>
> +class B {
> +  char const *get_p() { return p; }
> +};
>

In C++11 onwards, we should reject both of these in the template definition.

+
> Index: lib/Sema/SemaChecking.cpp
> ===================================================================
> --- lib/Sema/SemaChecking.cpp
> +++ lib/Sema/SemaChecking.cpp
> @@ -4592,7 +4592,6 @@
>    case Stmt::CXXReinterpretCastExprClass: {
>      Expr* SubExpr = cast<CastExpr>(E)->getSubExpr();
>      switch (cast<CastExpr>(E)->getCastKind()) {
> -    case CK_BitCast:
>      case CK_LValueToRValue:
>      case CK_NoOp:
>      case CK_BaseToDerived:
> @@ -4607,6 +4606,14 @@
>      case CK_ArrayToPointerDecay:
>        return EvalVal(SubExpr, refVars, ParentDecl);
>
> +    case CK_BitCast:
> +      if (SubExpr->getType()->isAnyPointerType() ||
> +          SubExpr->getType()->isBlockPointerType() ||
> +          SubExpr->getType()->isObjCQualifiedIdType())
> +        return EvalAddr(SubExpr, refVars, ParentDecl);
> +      else
> +        return EvalVal(SubExpr, refVars, ParentDecl);
>

Should Eval* really be walking into value-dependent expressions?

I think the right thing to do here is to fail if you don't have a pointer
(return nullptr), not to call EvalVal.

+
>      default:
>        return nullptr;
>      }
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140627/576622f9/attachment.html>


More information about the cfe-commits mailing list