summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/bugprone-misplaced-operator-in-strlen-in-alloc.cpp
diff options
context:
space:
mode:
authorAdam Balogh <adam.balogh@ericsson.com>2017-11-23 12:33:12 +0000
committerAdam Balogh <adam.balogh@ericsson.com>2017-11-23 12:33:12 +0000
commit4c488975daf72e5ffd3b57971950d9e563d65074 (patch)
tree14a270a50dd7c41085b59f7a3b5d5d8337359b6d /clang-tools-extra/test/clang-tidy/bugprone-misplaced-operator-in-strlen-in-alloc.cpp
parent2079defd8d13d6eebe7d902d08733afbc8198faf (diff)
downloadbcm5719-llvm-4c488975daf72e5ffd3b57971950d9e563d65074.tar.gz
bcm5719-llvm-4c488975daf72e5ffd3b57971950d9e563d65074.zip
[clang-tidy] Misplaced Operator in Strlen in Alloc
A possible error is to write `malloc(strlen(s+1))` instead of `malloc(strlen(s)+1)`. Unfortunately the former is also valid syntactically, but allocates less memory by two bytes (if `s` is at least one character long, undefined behavior otherwise) which may result in overflow cases. This check detects such cases and also suggests the fix for them. Fix for r318906, forgot to add new files. llvm-svn: 318907
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/bugprone-misplaced-operator-in-strlen-in-alloc.cpp')
-rw-r--r--clang-tools-extra/test/clang-tidy/bugprone-misplaced-operator-in-strlen-in-alloc.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/bugprone-misplaced-operator-in-strlen-in-alloc.cpp b/clang-tools-extra/test/clang-tidy/bugprone-misplaced-operator-in-strlen-in-alloc.cpp
new file mode 100644
index 00000000000..406b04348af
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/bugprone-misplaced-operator-in-strlen-in-alloc.cpp
@@ -0,0 +1,33 @@
+// RUN: %check_clang_tidy %s bugprone-misplaced-operator-in-strlen-in-alloc %t
+
+namespace std {
+typedef __typeof(sizeof(int)) size_t;
+void *malloc(size_t);
+
+size_t strlen(const char *);
+} // namespace std
+
+namespace non_std {
+typedef __typeof(sizeof(int)) size_t;
+void *malloc(size_t);
+
+size_t strlen(const char *);
+} // namespace non_std
+
+void bad_std_malloc_std_strlen(char *name) {
+ char *new_name = (char *)std::malloc(std::strlen(name + 1));
+ // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen
+ // CHECK-FIXES: {{^ char \*new_name = \(char \*\)std::malloc\(}}std::strlen(name) + 1{{\);$}}
+}
+
+void ignore_non_std_malloc_std_strlen(char *name) {
+ char *new_name = (char *)non_std::malloc(std::strlen(name + 1));
+ // CHECK-MESSAGES-NOT: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen
+ // Ignore functions of the malloc family in custom namespaces
+}
+
+void ignore_std_malloc_non_std_strlen(char *name) {
+ char *new_name = (char *)std::malloc(non_std::strlen(name + 1));
+ // CHECK-MESSAGES-NOT: :[[@LINE-1]]:28: warning: addition operator is applied to the argument of strlen
+ // Ignore functions of the strlen family in custom namespaces
+}
OpenPOWER on IntegriCloud