diff options
author | Chris Lattner <sabre@nondot.org> | 2005-05-20 22:22:25 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2005-05-20 22:22:25 +0000 |
commit | 937513599951540cfa01fe9c3d9ed60f19b2bf00 (patch) | |
tree | 01b21966a6fac81723586df1d850b4333c7286d4 | |
parent | 42eb7524ef3a994f184924e583871406cf5d1ed6 (diff) | |
download | external_llvm-937513599951540cfa01fe9c3d9ed60f19b2bf00.zip external_llvm-937513599951540cfa01fe9c3d9ed60f19b2bf00.tar.gz external_llvm-937513599951540cfa01fe9c3d9ed60f19b2bf00.tar.bz2 |
Fix mismatched type problem that crashed on cases like this:
sprintf(P, "%s", X);
Where X is not an sbyte*. This fixes the bug JohnMC reported on llvm-bugs.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@22159 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Transforms/IPO/SimplifyLibCalls.cpp | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/lib/Transforms/IPO/SimplifyLibCalls.cpp b/lib/Transforms/IPO/SimplifyLibCalls.cpp index 2e4d00d..127d8cc 100644 --- a/lib/Transforms/IPO/SimplifyLibCalls.cpp +++ b/lib/Transforms/IPO/SimplifyLibCalls.cpp @@ -542,7 +542,7 @@ public: return false; } - /// @brief Perform the strcpy optimization + /// @brief Perform the strchr optimizations virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) { // If there aren't three operands, bail @@ -625,7 +625,7 @@ public: "Number of 'strcmp' calls simplified") {} virtual ~StrCmpOptimization() {} - /// @brief Make sure that the "strcpy" function has the right prototype + /// @brief Make sure that the "strcmp" function has the right prototype virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) { if (f->getReturnType() == Type::IntTy && f->arg_size() == 2) @@ -633,7 +633,7 @@ public: return false; } - /// @brief Perform the strcpy optimization + /// @brief Perform the strcmp optimization virtual bool OptimizeCall(CallInst* ci, SimplifyLibCalls& SLC) { // First, check to see if src and destination are the same. If they are, @@ -710,7 +710,7 @@ public: "Number of 'strncmp' calls simplified") {} virtual ~StrNCmpOptimization() {} - /// @brief Make sure that the "strcpy" function has the right prototype + /// @brief Make sure that the "strncmp" function has the right prototype virtual bool ValidateCalledFunction(const Function* f, SimplifyLibCalls& SLC) { if (f->getReturnType() == Type::IntTy && f->arg_size() == 3) @@ -1366,6 +1366,15 @@ public: } } FPrintFOptimizer; +/// CastToCStr - Return V if it is an sbyte*, otherwise cast it to sbyte*, +/// inserting the cast before IP, and return the cast. +static Value *CastToCStr(Value *V, Instruction &IP) { + const Type *SBPTy = PointerType::get(Type::SByteTy); + if (V->getType() != SBPTy) + return new CastInst(V, SBPTy, V->getName(), &IP); + return V; +} + /// This LibCallOptimization will simplify calls to the "sprintf" library /// function. It looks for cases where the result of sprintf is not used and the @@ -1468,8 +1477,8 @@ public: if (!strcpy_func) return false; std::vector<Value*> args; - args.push_back(ci->getOperand(1)); - args.push_back(ci->getOperand(3)); + args.push_back(CastToCStr(ci->getOperand(1), *ci)); + args.push_back(CastToCStr(ci->getOperand(3), *ci)); new CallInst(strcpy_func,args,"",ci); } else if (getConstantStringLength(ci->getOperand(3),len)) @@ -1480,8 +1489,8 @@ public: if (!memcpy_func) return false; std::vector<Value*> args; - args.push_back(ci->getOperand(1)); - args.push_back(ci->getOperand(3)); + args.push_back(CastToCStr(ci->getOperand(1), *ci)); + args.push_back(CastToCStr(ci->getOperand(3), *ci)); args.push_back(ConstantUInt::get(Type::UIntTy,len)); args.push_back(ConstantUInt::get(Type::UIntTy,1)); new CallInst(memcpy_func,args,"",ci); |