Index: clang/lib/Analysis/UnsafeBufferUsage.cpp =================================================================== --- clang/lib/Analysis/UnsafeBufferUsage.cpp +++ clang/lib/Analysis/UnsafeBufferUsage.cpp @@ -164,23 +164,38 @@ // A UPC can be // 1. an argument of a function call (except the callee has [[unsafe_...]] // attribute), or - // 2. the operand of a cast operation; or + // 2. the operand of a cast-to-(Integer or Boolean) operation; or + // 3. the operand of a pointer subtraction operation + // (i.e., computing the distance between two pointers); or + // 4. the operand of a pointer comparison operation; or // ... auto CallArgMatcher = - callExpr(forEachArgumentWithParam(InnerMatcher, + callExpr(forEachArgumentWithParam(InnerMatcher, hasPointerType() /* array also decays to pointer type*/), unless(callee(functionDecl(hasAttr(attr::UnsafeBufferUsage))))); auto CastOperandMatcher = - explicitCastExpr(hasCastKind(CastKind::CK_PointerToIntegral), - castSubExpr(allOf(hasPointerType(), InnerMatcher))); + castExpr(anyOf(hasCastKind(CastKind::CK_PointerToIntegral), + hasCastKind(CastKind::CK_PointerToBoolean)), + castSubExpr(allOf(hasPointerType(), InnerMatcher))); + + // A matcher that matches pointer subtractions: + auto PtrSubtractionMatcher = + binaryOperator(hasOperatorName("-"), + // Note that here we need both LHS and RHS to be + // pointer. Then the inner matcher can match any of + // them: + allOf(hasLHS(hasPointerType()), + hasRHS(hasPointerType())), + eachOf(hasLHS(InnerMatcher), + hasRHS(InnerMatcher))); auto CompOperandMatcher = binaryOperator(hasAnyOperatorName("!=", "==", "<", "<=", ">", ">="), eachOf(hasLHS(allOf(hasPointerType(), InnerMatcher)), hasRHS(allOf(hasPointerType(), InnerMatcher)))); - return stmt(anyOf(CallArgMatcher, CastOperandMatcher, CompOperandMatcher)); + return stmt(anyOf(CallArgMatcher, CastOperandMatcher, CompOperandMatcher, PtrSubtractionMatcher)); // FIXME: any more cases? (UPC excludes the RHS of an assignment. For now we // don't have to check that.) } Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-addressof-arraysubscript.cpp =================================================================== --- clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-addressof-arraysubscript.cpp +++ clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-addressof-arraysubscript.cpp @@ -13,6 +13,24 @@ // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:37-[[@LINE-1]]:42}:"&p.data()[x]" } +void address_to_bool(int x) { + int * p = new int[10]; + bool a = (bool) &p[5]; + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:19-[[@LINE-1]]:24}:"&p.data()[5]" + bool b = (bool) &p[x]; + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:19-[[@LINE-1]]:24}:"&p.data()[x]" + + bool a1 = &p[5]; + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:18}:"&p.data()[5]" + bool b1 = &p[x]; + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:18}:"&p.data()[x]" + + if (&p[5]) { + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:12}:"&p.data()[5]" + return; + } +} + void call_argument(int x) { int * p = new int[10]; @@ -55,9 +73,26 @@ // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:5-[[@LINE-1]]:10}:"p.data()" } -// CHECK-NOT: fix-it +void pointer_subtraction(int x) { + int * p = new int[10]; + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:12}:"std::span p" + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{" + // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}" + + int n = &p[9] - &p[4]; + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:16}:"&p.data()[9]" + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:19-[[@LINE-2]]:24}:"&p.data()[4]" + if (&p[9] - &p[x]) { + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:12}:"&p.data()[9]" + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:20}:"&p.data()[x]" + return; + } +} + // To test multiple function declarations, each of which carries -// different incomplete informations: +// different incomplete informations. +// no fix-it in the rest of this test: + [[clang::unsafe_buffer_usage]] void unsafe_g(void*); @@ -65,8 +100,9 @@ void multiple_unsafe_fundecls() { int * p = new int[10]; - + // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]] unsafe_g(&p[5]); + // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]] } void unsafe_h(void*); @@ -78,6 +114,7 @@ void multiple_unsafe_fundecls2() { int * p = new int[10]; - + // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]] unsafe_h(&p[5]); + // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]] }