diff options
author | Jonas Toth <jonas.toth@gmail.com> | 2018-11-12 16:01:39 +0000 |
---|---|---|
committer | Jonas Toth <jonas.toth@gmail.com> | 2018-11-12 16:01:39 +0000 |
commit | 5315ced67a488e452d6665c509ce41a5c876da00 (patch) | |
tree | be7aea3459f2d61fe6ef7bee037aeea60788d3bc /clang-tools-extra/docs | |
parent | aa0cf14239530cc0ce24e95640379313a166ce3e (diff) |
[clang-tidy] new check: bugprone-too-small-loop-variable
The new checker searches for those for loops which has a loop variable with a "too small" type which means this type can't represent all values which are part of the iteration range.
For example:
```
int main() {
long size = 300000;
for( short int i = 0; i < size; ++i) {}
}
```
The short type leads to infinite loop here because it can't store all values in the `[0..size]` interval. In a real use case, size means a container's size which depends on the user input. Which means for small amount of objects the algorithm works, but with a larger user input the software will freeze.
The idea of the checker comes from the LibreOffice project, where the same check was implemented as a clang compiler plugin, called `LoopVarTooSmall` (LLVM licensed).
The idea is the same behind this check, but the code is different because of the different framework.
Patch by ztamas.
Reviewers: alexfh, hokein, aaron.ballman, JonasToth, xazax.hun, whisperity
Reviewed By: JonasToth, whisperity
Differential Revision: https://reviews.llvm.org/D53974
Diffstat (limited to 'clang-tools-extra/docs')
3 files changed, 37 insertions, 0 deletions
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 45dffb92978..17d6238b818 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -110,6 +110,13 @@ Improvements to clang-tidy Flags uses of ``absl::StrCat()`` to append to a ``std::string``. Suggests ``absl::StrAppend()`` should be used instead. +- New :doc:`bugprone-too-small-loop-variable + <clang-tidy/checks/bugprone-too-small-loop-variable>` check. + + Detects those ``for`` loops that have a loop variable with a "too small" type + which means this type can't represent all values which are part of the + iteration range. + - New :doc:`cppcoreguidelines-macro-usage <clang-tidy/checks/cppcoreguidelines-macro-usage>` check. diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone-too-small-loop-variable.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone-too-small-loop-variable.rst new file mode 100644 index 00000000000..7688a3a567e --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone-too-small-loop-variable.rst @@ -0,0 +1,29 @@ +.. title:: clang-tidy - bugprone-too-small-loop-variable + +bugprone-too-small-loop-variable +================================ + +Detects those ``for`` loops that have a loop variable with a "too small" type +which means this type can't represent all values which are part of the +iteration range. + +.. code-block:: c++ + + int main() { + long size = 294967296l; + for (short i = 0; i < size; ++i) {} + } + +This ``for`` loop is an infinite loop because the ``short`` type can't represent +all values in the ``[0..size]`` interval. + +In a real use case size means a container's size which depends on the user input. + +.. code-block:: c++ + + int doSomething(const std::vector& items) { + for (short i = 0; i < items.size(); ++i) {} + } + +This algorithm works for small amount of objects, but will lead to freeze for a +a larger user input. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index f33130b5e46..1d5468f47b9 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -59,6 +59,7 @@ Clang-Tidy Checks bugprone-swapped-arguments bugprone-terminating-continue bugprone-throw-keyword-missing + bugprone-too-small-loop-variable bugprone-undefined-memory-manipulation bugprone-undelegated-constructor bugprone-unused-raii |