aboutsummaryrefslogtreecommitdiff
path: root/lib/Analysis
diff options
context:
space:
mode:
authorNuno Lopes <nunoplopes@sapo.pt>2012-07-24 16:28:13 +0000
committerNuno Lopes <nunoplopes@sapo.pt>2012-07-24 16:28:13 +0000
commit9827c8e1c96950d17a4dbb7ef9d9036501c40c1b (patch)
treeadc0338a9d54072cba27b1dd313a112dde05cfb0 /lib/Analysis
parenta94d6e87c4c49f2e81b01d66d8bfb591277f8f96 (diff)
teach objectsize about strdup() and strndup()
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@160676 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis')
-rw-r--r--lib/Analysis/MemoryBuiltins.cpp19
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/Analysis/MemoryBuiltins.cpp b/lib/Analysis/MemoryBuiltins.cpp
index 8d99ec3e56..4833b5212b 100644
--- a/lib/Analysis/MemoryBuiltins.cpp
+++ b/lib/Analysis/MemoryBuiltins.cpp
@@ -64,7 +64,7 @@ static const AllocFnsTy AllocationFnData[] = {
{"realloc", ReallocLike, 2, 1, -1},
{"reallocf", ReallocLike, 2, 1, -1},
{"strdup", StrDupLike, 1, -1, -1},
- {"strndup", StrDupLike, 2, -1, -1}
+ {"strndup", StrDupLike, 2, 1, -1}
};
@@ -414,8 +414,21 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
// handle strdup-like functions separately
if (FnData->AllocTy == StrDupLike) {
- // TODO
- return unknown();
+ APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
+ if (!Size)
+ return unknown();
+
+ // strndup limits strlen
+ if (FnData->FstParam > 0) {
+ ConstantInt *Arg= dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
+ if (!Arg)
+ return unknown();
+
+ APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
+ if (Size.ugt(MaxSize))
+ Size = MaxSize + 1;
+ }
+ return std::make_pair(Size, Zero);
}
ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));