Skip to content

Commit

Permalink
[clang-tidy] Fix crash in modernize-loop-convert when int is used as …
Browse files Browse the repository at this point in the history
…iterator (#78796)

Fix crash when built-in type (like int) is used as iterator, or when
call to begin() return integer.

Closes #78381
  • Loading branch information
PiotrZSL committed Jan 20, 2024
1 parent 4b50014 commit 296fbee
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
12 changes: 8 additions & 4 deletions clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,11 +942,15 @@ bool LoopConvertCheck::isConvertible(ASTContext *Context,
CanonicalInitVarType->isPointerType()) {
// If the initializer and the variable are both pointers check if the
// un-qualified pointee types match, otherwise we don't use auto.
if (!Context->hasSameUnqualifiedType(
CanonicalBeginType->getPointeeType(),
CanonicalInitVarType->getPointeeType()))
return false;
return Context->hasSameUnqualifiedType(
CanonicalBeginType->getPointeeType(),
CanonicalInitVarType->getPointeeType());
}

if (CanonicalBeginType->isBuiltinType() ||
CanonicalInitVarType->isBuiltinType())
return false;

} else if (FixerKind == LFK_PseudoArray) {
if (const auto *EndCall = Nodes.getNodeAs<CXXMemberCallExpr>(EndCallName)) {
// This call is required to obtain the container.
Expand Down
5 changes: 3 additions & 2 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ Changes in existing checks
- Improved :doc:`modernize-loop-convert
<clang-tidy/checks/modernize/loop-convert>` to support for-loops with
iterators initialized by free functions like ``begin``, ``end``, or ``size``
and avoid crash for array of dependent array.
and avoid crash for array of dependent array and non-dereferenceable builtin
types used as iterators.

- Improved :doc:`modernize-make-shared
<clang-tidy/checks/modernize/make-shared>` check to support
Expand Down Expand Up @@ -502,7 +503,7 @@ Changes in existing checks
<clang-tidy/checks/readability/implicit-bool-conversion>` check to take
do-while loops into account for the `AllowIntegerConditions` and
`AllowPointerConditions` options. It also now provides more consistent
suggestions when parentheses are added to the return value or expressions.
suggestions when parentheses are added to the return value or expressions.
It also ignores false-positives for comparison containing bool bitfield.

- Improved :doc:`readability-misleading-indentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,3 +954,16 @@ void dependenceArrayTest() {
}

} // namespace PseudoArray

namespace PR78381 {
struct blocked_range {
int begin() const;
int end() const;
};

void test() {
blocked_range r;
for (auto i = r.begin(); i!=r.end(); ++i) {
}
}
}

0 comments on commit 296fbee

Please sign in to comment.