# HG changeset patch # User Chris Peterson # Date 1531631232 25200 # Sat Jul 14 22:07:12 2018 -0700 # Node ID 8a9e7ed36126dd6f9454a58b732dcec308805558 # Parent 21408667426c71a87770c4422400b7da004d3f1c Bug 1475882 - clang-analyzer: Enable clang-analyzer-unix.cstring.NullArg check. r=andi Check for null pointers being passed as arguments to C string functions. There are no clang-analyzer-unix.cstring.NullArg warnings in mozilla-central! strlen strnlen strcpy strncpy strcat strncat strcmp strncmp strcasecmp strncasecmp https://clang-analyzer.llvm.org/available_checks.html MozReview-Commit-ID: EkfaItfo5cu diff --git a/tools/clang-tidy/config.yaml b/tools/clang-tidy/config.yaml --- a/tools/clang-tidy/config.yaml +++ b/tools/clang-tidy/config.yaml @@ -31,16 +31,18 @@ clang_checkers: - name: clang-analyzer-security.insecureAPI.strcpy publish: !!bool no - name: clang-analyzer-security.insecureAPI.UncheckedReturn publish: !!bool yes - name: clang-analyzer-security.insecureAPI.vfork publish: !!bool yes - name: clang-analyzer-unix.cstring.BadSizeArg publish: !!bool yes + - name: clang-analyzer-unix.cstring.NullArg + publish: !!bool yes - name: misc-argument-comment publish: !!bool yes - name: misc-assert-side-effect publish: !!bool yes - name: misc-bool-pointer-implicit-conversion publish: !!bool yes - name: misc-forward-declaration-namespace # Name with clang tidy 6.0. We are currently using 5.0 diff --git a/tools/clang-tidy/test/clang-analyzer-unix.cstring.NullArg.cpp b/tools/clang-tidy/test/clang-analyzer-unix.cstring.NullArg.cpp new file mode 100644 --- /dev/null +++ b/tools/clang-tidy/test/clang-analyzer-unix.cstring.NullArg.cpp @@ -0,0 +1,14 @@ +// https://clang-analyzer.llvm.org/available_checks.html + +#include "structures.h" + +int my_strlen(const char* s) +{ + return strlen(s); // warning +} + +int bad_caller() +{ + const char* s = nullptr; + return my_strlen(s); +} diff --git a/tools/clang-tidy/test/clang-analyzer-unix.cstring.NullArg.json b/tools/clang-tidy/test/clang-analyzer-unix.cstring.NullArg.json new file mode 100644 --- /dev/null +++ b/tools/clang-tidy/test/clang-analyzer-unix.cstring.NullArg.json @@ -0,0 +1,1 @@ +"[[\"warning\", \"Null pointer argument in call to string length function\", \"clang-analyzer-unix.cstring.NullArg\"]]" \ No newline at end of file diff --git a/tools/clang-tidy/test/structures.h b/tools/clang-tidy/test/structures.h --- a/tools/clang-tidy/test/structures.h +++ b/tools/clang-tidy/test/structures.h @@ -83,9 +83,10 @@ char *mktemp(char *tmpl); pid_t vfork(void); int abort() { return 0; } #define assert(x) \ if (!(x)) \ (void)abort() +std::size_t strlen(const char *s); char *strncat(char *s1, const char *s2, std::size_t n);