]> granicus.if.org Git - clang/commitdiff
__builtin_object_size refinements. When we run out of object, be sure
authorMike Stump <mrs@apple.com>
Mon, 26 Oct 2009 21:38:39 +0000 (21:38 +0000)
committerMike Stump <mrs@apple.com>
Mon, 26 Oct 2009 21:38:39 +0000 (21:38 +0000)
to clamp at 0 bytes left.  WIP.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@85157 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/ExprConstant.cpp
test/CodeGen/object-size.c

index f1a446ef6e748d60d1cb879abbe0f3a3b1f10760..9992b6944ec35b58bc9e9c5b5b9d4c4ad5e4b451 100644 (file)
@@ -884,7 +884,11 @@ bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
         if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
           if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
             uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
-            Size -= Base.Val.getLValueOffset();
+            uint64_t Offset = Base.Val.getLValueOffset();
+            if (Offset <= Size)
+              Size -= Base.Val.getLValueOffset();
+            else
+              Size = 0;
             return Success(Size, E);
           }
         }
index b686116989343c3059c4388a49f089a71ccd9aa4..36dc341e932785301d6c8337e86bb32e54cbf51e 100644 (file)
@@ -21,13 +21,29 @@ void test2() {
   strcpy(gbuf, "Hi there");
 }
 
+void test3() {
+  // CHECK:       movabsq $0, %rdx
+  // CHECK-NEXT:  movq    %rax, %rdi
+  // CHECK-NEXT:  movq    %rcx, %rsi
+  // CHECK-NEXT:  call    ___strcpy_chk
+  strcpy(&gbuf[100], "Hi there");
+}
+
 void test4() {
+  // CHECK:       movabsq $0, %rdx
+  // CHECK-NEXT:  movq    %rax, %rdi
+  // CHECK-NEXT:  movq    %rcx, %rsi
+  // CHECK-NEXT:  call    ___strcpy_chk
+  strcpy((char*)(void*)&gbuf[-1], "Hi there");
+}
+
+void test5() {
   // CHECK:       call    ___inline_strcpy_chk
-  strcpy(gp, "Hi");
+  strcpy(gp, "Hi there");
 }
 
-void test3() {
+void test6() {
   int i;
   // CHECK:       call    ___inline_strcpy_chk
-  strcpy((++i, gbuf), "Hi");
+  strcpy((++i, gbuf), "Hi there");
 }