This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] add check cppcoreguidelines-pro-bounds-constant-array-index
ClosedPublic

Authored by mgehre on Nov 26 2015, 2:50 PM.

Details

Summary

This is http://reviews.llvm.org/D13746 but instead of including <array>,
a stub is provided.
This check flags all array subscriptions on static arrays and
std::arrays that either have a non-compile-time-constant index or are
out of bounds.

Dynamic accesses into arrays are difficult for both tools and humans to
validate as safe. array_view is a bounds-checked, safe type for
accessing arrays of data. at() is another alternative that ensures
single accesses are bounds-checked. If iterators are needed to access an
array, use the iterators from an array_view constructed over the array.

This rule is part of the "Bounds safety" profile of the C++ Core
Guidelines, see
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-bounds2-only-index-into-arrays-using-constant-expressions

Diff Detail

Event Timeline

mgehre updated this revision to Diff 41274.Nov 26 2015, 2:50 PM
mgehre retitled this revision from to [clang-tidy] add check cppcoreguidelines-pro-bounds-constant-array-index.
mgehre updated this object.
mgehre added a subscriber: cfe-commits.
aaron.ballman added inline comments.Dec 4 2015, 9:19 AM
clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp
30

Why GslHeader but not IncludeStyle?

80

instead of "compile-time constant", we should use "integer constant expression".

90

Please do not use auto here, it is not obvious what type will be returned.

93

Can just use if (Insertion) instead.

108

"index %0 is negative" is shorter and should be obvious as to what the issue is.

113

Please do not use auto here either.

docs/clang-tidy/checks/cppcoreguidelines-pro-bounds-constant-array-index.rst
5

Instead of "array subscriptions" it should be "array subscript expressions". Can also change "non-compile-time constant" to "that either do not have a constant integer expression"

8

I'm not comfortable with directly copying the text from the C++ Core Guidelines into our own documentation. I don't believe their license allows this.

10

s/generated/generate

test/clang-tidy/cppcoreguidelines-pro-bounds-constant-array-index.cpp
2

We should have an additional test that does not have the GslHeader option set as well.

alexfh edited edge metadata.Dec 4 2015, 1:09 PM

Sorry for the delay. Missed this patch somehow. A couple of comments in addition to what Aaron wrote.

clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.h
26

This can be const.

docs/clang-tidy/checks/cppcoreguidelines-pro-bounds-constant-array-index.rst
5

nit: I believe, .rst files should also have the 80 columns limit.

mgehre updated this revision to Diff 42338.Dec 9 2015, 2:09 PM
mgehre edited edge metadata.

Thanks for the comments!

One more nit. Leaving the rest to Aaron, since most of the comments were his.

clang-tidy/cppcoreguidelines/ProBoundsConstantArrayIndexCheck.h
25

GslHeader can be const as well.

aaron.ballman accepted this revision.Dec 10 2015, 11:44 AM
aaron.ballman edited edge metadata.

With the fix for Alex's last comment, LGTM!

This revision is now accepted and ready to land.Dec 10 2015, 11:44 AM
This revision was automatically updated to reflect the committed changes.