From 3c5be6c9a72bad003348459a578b6b37678b075e Mon Sep 17 00:00:00 2001 From: Etienne Bergeron Date: Fri, 15 Apr 2016 16:31:15 +0000 Subject: [clang-tidy] Add checker for operations between integrals and pointers Summary: This check is finding suspicious operations involving pointers and integral types; which are most likely bugs. Examples: subversion/v1_6/subversion/libsvn_subr/utf.c ``` static const char * fuzzy_escape(const char *src, apr_size_t len, apr_pool_t *pool) { [...] while (src_orig < src_end) { if (! svn_ctype_isascii(*src_orig) || src_orig == '\0') // Should be *src_orig { ``` apache2/v2_2_23/modules/metadata/mod_headers.c ``` static char *parse_format_tag(apr_pool_t *p, format_tag *tag, const char **sa) { [...] tag->arg = '\0'; // ERROR: tag->arg has type char* /* grab the argument if there is one */ if (*s == '{') { ++s; tag->arg = ap_getword(p,&s,'}'); } ``` Reviewers: alexfh Subscribers: Eugene.Zelenko, cfe-commits Differential Revision: http://reviews.llvm.org/D19118 llvm-svn: 266450 --- .../misc-pointer-and-integral-operation.cpp | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 clang-tools-extra/test/clang-tidy/misc-pointer-and-integral-operation.cpp (limited to 'clang-tools-extra/test/clang-tidy/misc-pointer-and-integral-operation.cpp') diff --git a/clang-tools-extra/test/clang-tidy/misc-pointer-and-integral-operation.cpp b/clang-tools-extra/test/clang-tidy/misc-pointer-and-integral-operation.cpp new file mode 100644 index 00000000000..6591f0af360 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/misc-pointer-and-integral-operation.cpp @@ -0,0 +1,30 @@ +// RUN: %check_clang_tidy %s misc-pointer-and-integral-operation %t + +bool* pb; +char* pc; +int* pi; + +int Test() { + if (pi < 0) return 0; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious comparison of pointer with zero [misc-pointer-and-integral-operation] + if (pi <= 0) return 0; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious comparison of pointer with zero + + if (nullptr <= pb) return 0; + // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: suspicious comparison of pointer with null + if (pc < nullptr) return 0; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious comparison of pointer with null + if (pi > nullptr) return 0; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious comparison of pointer with null + + return 1; +} + +int Valid() { + *pb = false; + *pc = '\0'; + + pi += (pi != nullptr); + pi -= (pi == nullptr); + pc += (pb != nullptr); +} -- cgit v1.2.3