[PATCH] D39121: [clang-tidy] Misplaced Operator in Strlen in Alloc

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 20 07:38:54 PDT 2017


aaron.ballman added inline comments.


================
Comment at: docs/clang-tidy/checks/misc-misplaced-operator-in-strlen-in-alloc.rst:16
+    void bad_malloc(char *str) {
+      char *c = (char*) malloc(strlen(str + 1));
+    }
----------------
xazax.hun wrote:
> What if this code is intentional for some reason?
> I think in thase case it could be rewritten like the following (which is much cleaner):
> ```
> char *c = (char*) malloc(strlen(str)-1);
> ```
> I think it might be a good idea to mention this possibility as a way to suppress this warning in the documentation. 
This is my concern as well -- I'd like to know how chatty this diagnostic is on real-world code bases, especially ones that rely on C rather than C++. A somewhat common code pattern in Win32 coding are double-null-terminated lists of strings, where you have null terminated strings at adjacent memory locations with two null characters for the end of the list. This could result in reasonable code like `malloc(strlen(str + offset) + 1)`.


https://reviews.llvm.org/D39121





More information about the cfe-commits mailing list