return {};
}
};
+
+/// A pointer arithmetic expression of one of the forms:
+/// \code
+/// ptr + n | n + ptr | ptr - n | ptr += n | ptr -= n
+/// \endcode
+class PointerArithmeticGadget : public UnsafeGadget {
+ static constexpr const char *const PointerArithmeticTag = "ptrAdd";
+ static constexpr const char *const PointerArithmeticPointerTag = "ptrAddPtr";
+ const BinaryOperator *PA; // pointer arithmetic expression
+ const Expr * Ptr; // the pointer expression in `PA`
+
+public:
+ PointerArithmeticGadget(const MatchFinder::MatchResult &Result)
+ : UnsafeGadget(Kind::PointerArithmetic),
+ PA(Result.Nodes.getNodeAs<BinaryOperator>(PointerArithmeticTag)),
+ Ptr(Result.Nodes.getNodeAs<Expr>(PointerArithmeticPointerTag)) {}
+
+ static bool classof(const Gadget *G) {
+ return G->getKind() == Kind::PointerArithmetic;
+ }
+
+ static Matcher matcher() {
+ auto HasIntegerType = anyOf(
+ hasType(isInteger()), hasType(enumType()));
+ auto PtrAtRight = allOf(hasOperatorName("+"),
+ hasRHS(expr(hasPointerType()).bind(PointerArithmeticPointerTag)),
+ hasLHS(HasIntegerType));
+ auto PtrAtLeft = allOf(
+ anyOf(hasOperatorName("+"), hasOperatorName("-"),
+ hasOperatorName("+="), hasOperatorName("-=")),
+ hasLHS(expr(hasPointerType()).bind(PointerArithmeticPointerTag)),
+ hasRHS(HasIntegerType));
+
+ return stmt(binaryOperator(anyOf(PtrAtLeft, PtrAtRight)).bind(PointerArithmeticTag));
+ }
+
+ const Stmt *getBaseStmt() const override { return PA; }
+
+ DeclUseList getClaimedVarUseSites() const override {
+ if (const auto *DRE =
+ dyn_cast<DeclRefExpr>(Ptr->IgnoreParenImpCasts())) {
+ return {DRE};
+ }
+
+ return {};
+ }
+ // FIXME: pointer adding zero should be fine
+ //FIXME: this gadge will need a fix-it
+};
} // namespace
namespace {
return &t[1]; // expected-warning{{unchecked operation on raw buffer in expression}}
}
+// Testing pointer arithmetic for pointer-to-int, qualified multi-level
+// pointer, pointer to a template type, and auto type
+T_ptr_t getPtr();
+
+template<typename T>
+void testPointerArithmetic(int * p, const int **q, T * x) {
+ int a[10];
+ auto y = &a[0];
+
+ foo(p + 1, 1 + p, p - 1, // expected-warning3{{unchecked operation on raw buffer in expression}}
+ *q + 1, 1 + *q, *q - 1, // expected-warning3{{unchecked operation on raw buffer in expression}}
+ x + 1, 1 + x, x - 1, // expected-warning3{{unchecked operation on raw buffer in expression}}
+ y + 1, 1 + y, y - 1, // expected-warning3{{unchecked operation on raw buffer in expression}}
+ getPtr() + 1, 1 + getPtr(), getPtr() - 1 // expected-warning3{{unchecked operation on raw buffer in expression}}
+ );
+
+ p += 1; p -= 1; // expected-warning2{{unchecked operation on raw buffer in expression}}
+ *q += 1; *q -= 1; // expected-warning2{{unchecked operation on raw buffer in expression}}
+ y += 1; y -= 1; // expected-warning2{{unchecked operation on raw buffer in expression}}
+ x += 1; x -= 1; // expected-warning2{{unchecked operation on raw buffer in expression}}
+}
+
void testTemplate(int * p) {
int *a[10];
foo(f(p, &p, a, a)[1]); // expected-warning{{unchecked operation on raw buffer in expression}}, \
expected-note{{in instantiation of function template specialization 'f<int *, 10>' requested here}}
+
+ const int **q = const_cast<const int **>(&p);
+
+ testPointerArithmetic(p, q, p); //expected-note{{in instantiation of function template specialization 'testPointerArithmetic<int>' requested here}}
}
void testPointerToMember() {