This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add support for operator new[] in check bugprone-misplaced-operator-in-strlen-in-alloc
ClosedPublic

Authored by baloghadamsoftware on Oct 27 2017, 2:20 AM.

Details

Summary

The check now recognizes error cases like new char[strlen(s + 1)] and suggests a fix in the format new char[strlen(s) + 1].

Diff Detail

Event Timeline

JonasToth set the repository for this revision to rL LLVM.
JonasToth added a project: Restricted Project.
JonasToth added a subscriber: JonasToth.

Should the release notes be modified as well?

JonasToth added inline comments.Oct 27 2017, 4:31 AM
clang-tidy/bugprone/MisplacedOperatorInStrlenInAllocCheck.cpp
62

Break after if condition. You could use the ternary operator, too.

Maybe a safeguard assertion if Alloc is nullptr after the second assignemt. This would uncover a potential bug.

Reformatted according to the comments.

baloghadamsoftware marked an inline comment as done.Oct 27 2017, 6:03 AM

Should the release notes be modified as well?

I think so. I consider the inclusion of operator new[] important.

aaron.ballman added inline comments.Oct 27 2017, 6:08 AM
clang-tidy/bugprone/MisplacedOperatorInStrlenInAllocCheck.cpp
64–65

The backticks should be single quotes instead, I think.

test/clang-tidy/bugprone-misplaced-operator-in-strlen-in-alloc.cpp
37

Please add tests showing that the correct behavior does not diagnose. Also, please add a test showing that this works with overloaded operator new[]().

Backsticks changed to single quotes, new tests added.

aaron.ballman added inline comments.Oct 27 2017, 7:31 AM
clang-tidy/bugprone/MisplacedOperatorInStrlenInAllocCheck.cpp
64–65

Missed the backticks around CXXNewExpr

baloghadamsoftware marked 3 inline comments as done.Oct 30 2017, 1:29 AM
This revision is now accepted and ready to land.Oct 30 2017, 9:22 AM