r214281 - Fix a use after free bug.

David Blaikie dblaikie at gmail.com
Tue Jul 29 20:38:30 PDT 2014


On Tue, Jul 29, 2014 at 7:37 PM, Rafael Espindola
<rafael.espindola at gmail.com> wrote:
> Author: rafael
> Date: Tue Jul 29 21:37:26 2014
> New Revision: 214281
>
> URL: http://llvm.org/viewvc/llvm-project?rev=214281&view=rev
> Log:
> Fix a use after free bug.
>
> Modified:
>     cfe/trunk/lib/AST/ASTContext.cpp
>
> Modified: cfe/trunk/lib/AST/ASTContext.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=214281&r1=214280&r2=214281&view=diff
> ==============================================================================
> --- cfe/trunk/lib/AST/ASTContext.cpp (original)
> +++ cfe/trunk/lib/AST/ASTContext.cpp Tue Jul 29 21:37:26 2014
> @@ -1432,11 +1432,14 @@ bool ASTContext::isAlignmentRequired(Qua
>  }
>
>  TypeInfo ASTContext::getTypeInfo(const Type *T) const {
> -  TypeInfo &TI = MemoizedTypeInfo[T];
> -  if (!TI.Align)
> -    TI = getTypeInfoImpl(T);
> +  TypeInfo TI = MemoizedTypeInfo[T];
> +  if (TI.Align)
> +    return TI;

You could keep TI as a reference, since you don't use it after the
getTypeInfoImpl call. Or you could use "TI" istead of "Temp" below,
and optionally put it in the if, like the original code:

TypeInfo TI = MemoizedTypeInfo[T]
if (!TI.Align) {
  TI = getTypeInfoImpl(T);
  MemoizedTypeInfo[T] = TI;
}
return TI;

which is a smaller change & more compact code. (but yeah, it's always
a toss up between reduced indentation by having an early return, and
increased indentation of nesting the whole "didn't find the element,
so create and insert one" path in the if - in this case, since the
whole implementation is really in another function anyway, that seems
OK)

- David

>
> -  return TI;
> +  // This call can invalidate TI, so we need a second lookup.
> +  TypeInfo Temp = getTypeInfoImpl(T);
> +  MemoizedTypeInfo[T] = Temp;
> +  return Temp;
>  }
>
>  /// getTypeInfoImpl - Return the size of the specified type, in bits.  This
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits



More information about the cfe-commits mailing list