[cfe-commits] r94622 - in /cfe/trunk: include/clang/Analysis/Support/Optional.h lib/Checker/RegionStore.cpp test/Analysis/misc-ps-region-store.m

Chandler Carruth chandlerc at google.com
Wed Jan 27 02:24:27 PST 2010


On Tue, Jan 26, 2010 at 3:51 PM, Ted Kremenek <kremenek at apple.com> wrote:
> ==============================================================================
> --- cfe/trunk/lib/Checker/RegionStore.cpp (original)
> +++ cfe/trunk/lib/Checker/RegionStore.cpp Tue Jan 26 17:51:00 2010
> @@ -1572,13 +1572,16 @@
>  const GRState *RegionStoreManager::BindArray(const GRState *state,
>                                              const TypedRegion* R,
>                                              SVal Init) {
> -
> -  QualType T = R->getValueType(getContext());
> -  ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
> -  QualType ElementTy = CAT->getElementType();
> -
> -  uint64_t size = CAT->getSize().getZExtValue();
> -
> +
> +  ASTContext &Ctx = getContext();
> +  const ArrayType *AT =
> +    cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
> +  QualType ElementTy = AT->getElementType();
> +  Optional<uint64_t> Size;

This form of Optional<> doesn't seem to be used elsewhere, and it
triggers a possibly used without being initialized warning in GCC.
I've silenced this by initializing the value in Optional, but let me
know if you prefer a different solution. It seems very unlikely to be
a problem as it was getting  default constructed anyways for any
non-primitive types.

> +
> +  if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
> +    Size = CAT->getSize().getZExtValue();
> +
>   // Check if the init expr is a StringLiteral.
>   if (isa<loc::MemRegionVal>(Init)) {
>     const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
> @@ -1590,6 +1593,11 @@
>     // Copy bytes from the string literal into the target array. Trailing bytes
>     // in the array that are not covered by the string literal are initialized
>     // to zero.
> +
> +    // We assume that string constants are bound to
> +    // constant arrays.
> +    uint64_t size = Size;

Did you mean Size.getValue()? I think this is implicitly converting from bool.

> +
>     for (uint64_t i = 0; i < size; ++i, ++j) {
>       if (j >= len)
>         break;
> @@ -1618,7 +1626,7 @@
>   nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
>   uint64_t i = 0;
>
> -  for (; i < size; ++i, ++VI) {
> +  for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
>     // The init list might be shorter than the array length.
>     if (VI == VE)
>       break;
> @@ -1626,16 +1634,15 @@
>     SVal Idx = ValMgr.makeArrayIndex(i);
>     const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
>
> -    if (CAT->getElementType()->isStructureType())
> +    if (ElementTy->isStructureType())
>       state = BindStruct(state, ER, *VI);
>     else
> -      // FIXME: Do we need special handling of nested arrays?
>       state = Bind(state, ValMgr.makeLoc(ER), *VI);
>   }
>
>   // If the init list is shorter than the array length, set the
>   // array default value.
> -  if (i < size)
> +  if (Size.hasValue() && i < Size.getValue())
>     state = setImplicitDefaultValue(state, R, ElementTy);
>
>   return state;
>
> Modified: cfe/trunk/test/Analysis/misc-ps-region-store.m
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/misc-ps-region-store.m?rev=94622&r1=94621&r2=94622&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/Analysis/misc-ps-region-store.m (original)
> +++ cfe/trunk/test/Analysis/misc-ps-region-store.m Tue Jan 26 17:51:00 2010
> @@ -730,3 +730,31 @@
>   }
>  }
>
> +//===----------------------------------------------------------------------===//
> +// <rdar://problem/7515938> - Handle initialization of incomplete arrays
> +//  in structures using a compound value.  Previously this crashed.
> +//===----------------------------------------------------------------------===//
> +
> +struct rdar_7515938 {
> +  int x;
> +  int y[];
> +};
> +
> +const struct rdar_7515938 *rdar_7515938() {
> +  static const struct rdar_7515938 z = { 0, { 1, 2 } };
> +  if (z.y[0] != 1) {
> +    int *p = 0;
> +    *p = 0xDEADBEEF; // no-warning
> +  }
> +  return &z;
> +}
> +
> +struct rdar_7515938_str {
> +  int x;
> +  char y[];
> +};
> +
> +const struct rdar_7515938_str *rdar_7515938_str() {
> +  static const struct rdar_7515938_str z = { 0, "hello" };
> +  return &z;
> +}
>
>
> _______________________________________________
> 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