Some programmers tend to forget that subtracting two pointers results in the difference between them in number of elements of the pointee type instead of bytes. This leads to codes such as size_t size = (p - q) / sizeof(int) where p and q are of type int*. Or similarily, if (p - q < buffer_size * sizeof(int)) { ... }. This patch extends bugprone-sizeof-expression to detect such cases.
Details
Details
- Reviewers
alexfh aaron.ballman lebedev.ri - Commits
- rZORGa59bc937a41e: [clang-tidy] Extend bugprone-sizeof-expression check to detect sizeof misuse in…
rZORGadf1b618d7a6: [clang-tidy] Extend bugprone-sizeof-expression check to detect sizeof misuse in…
rGa59bc937a41e: [clang-tidy] Extend bugprone-sizeof-expression check to detect sizeof misuse in…
rGadf1b618d7a6: [clang-tidy] Extend bugprone-sizeof-expression check to detect sizeof misuse in…
rG62468003ef97: [clang-tidy] Extend bugprone-sizeof-expression check to detect sizeof misuse in…
rL360032: [clang-tidy] Extend bugprone-sizeof-expression check to detect sizeof misuse in…
rCTE360032: [clang-tidy] Extend bugprone-sizeof-expression check to detect sizeof misuse in…
Diff Detail
Diff Detail
- Repository
- rCTE Clang Tools Extra
Event Timeline
Comment Actions
Type int in tests replaced by struct S because it has more "sugar". Check also fixed to handle this case.
Comment Actions
Out of curiosity, have you run this over any large code bases to see what the false positive and true positive rate is?
clang-tidy/bugprone/SizeofExpressionCheck.cpp | ||
---|---|---|
217 | Missing full stop at the end of the comment. |
Comment Actions
Neither false, nor true positives found so far. I ran it on several open-source projects.
Comment Actions
Hmm, I am a little bit skeptical about the utility of this compared to the expense of making the check marginally slower. However, I don't have any specific objections and so I'm not opposed to the patch. LG!
Comment Actions
I think that the open-source projects I used for testing are mature enough for such amateur errors. However many beginner programmers make such errors.
Missing full stop at the end of the comment.