[llvm-commits] [PATCH] MemCpyOptimizer: fix invalid reads shown by valgrind

Chris Lattner clattner at apple.com
Sat May 3 16:46:46 PDT 2008


On May 3, 2008, at 6:24 AM, Török Edwin wrote:

> Hi,
>
> Valgrind has shown that MemCpyOptimizer is accessing freed memory, see
> relevant portions below.
> The instruction is freed in processStore using eraseFromParent here:
> if (StoreInst *SI = dyn_cast<StoreInst>(I))
>        changed_function |= processStore(SI, BI);
>
> But dyn_cast<> is accessing the freed instruction here:
>    if (MemCpyInst* M = dyn_cast<MemCpyInst>(I)) {
>
> Since StoreInst can never be a MemCpyInst a simple solution is to use
> 'else'.
> Attached patch fixes this, ok to commit?

Looks great, please commit!

-Chris

>
>
> --Edwin
> Index: lib/Transforms/Scalar/MemCpyOptimizer.cpp
> ===================================================================
> --- lib/Transforms/Scalar/MemCpyOptimizer.cpp	(revision 50604)
> +++ lib/Transforms/Scalar/MemCpyOptimizer.cpp	(working copy)
> @@ -718,8 +718,7 @@
>
>       if (StoreInst *SI = dyn_cast<StoreInst>(I))
>         changed_function |= processStore(SI, BI);
> -
> -      if (MemCpyInst* M = dyn_cast<MemCpyInst>(I)) {
> +      else if (MemCpyInst* M = dyn_cast<MemCpyInst>(I)) {
>         changed_function |= processMemCpy(M);
>       }
>     }
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits





More information about the llvm-commits mailing list