This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix a potential infinite loop in readability-isolate-declaration check.
ClosedPublic

Authored by hokein on Sep 17 2019, 4:39 AM.

Diff Detail

Repository
rL LLVM

Event Timeline

hokein created this revision.Sep 17 2019, 4:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 17 2019, 4:39 AM
Herald added a subscriber: xazax.hun. · View Herald Transcript

Do we have unit tests for clang-tidy? If yes, could we also add unit-tests for this function?

The modified function only needs a source manager and lang options, these are easy to mock.

clang-tools-extra/clang-tidy/utils/LexerUtils.h
38 ↗(On Diff #220473)

Would findPrevious have the same problem on the first token of the file?
Can be hard to check without unit-tests, though.

66 ↗(On Diff #220473)

We seem to be missing a corner case here: what if folks are searching for 'tok::eof'?

Currently we would return invalid location, but given the function's signature I'd expect it to return location of 'tok::eof' in that case.

I bet we don't run into this in practice, though.

ilya-biryukov accepted this revision.Sep 18 2019, 1:23 AM

The fix itself LGTM, getting rid of an infinite loop is important.
All my comment can be addressed later...

This revision is now accepted and ready to land.Sep 18 2019, 1:23 AM
hokein updated this revision to Diff 220627.Sep 18 2019, 2:08 AM
hokein marked 2 inline comments as done.

Handle the eof corner case.

Do we have unit tests for clang-tidy? If yes, could we also add unit-tests for this function?
The modified function only needs a source manager and lang options, these are easy to mock.

Unfortunately, we don't have any unit tests for all these utility functions. The modified function is only used in the readability-isolate-declaration check.

I agree that we should add one, will file a bug.

clang-tools-extra/clang-tidy/utils/LexerUtils.h
38 ↗(On Diff #220473)

it depends on the caller. Calling this function directly would probably get into infinite loop. As this function is only used in readability-isolate-declaration check, it seems that the check adds some additional guard code before calling this function, it is probably safe, I assume. (I could not figure out a case that causes the problem).

66 ↗(On Diff #220473)

yeah, I thought that we shouldn't run into this corner case, I tweaked the code to fix this case.

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptSep 18 2019, 2:20 AM
ilya-biryukov marked an inline comment as done.Sep 18 2019, 8:43 AM
ilya-biryukov added inline comments.
clang-tools-extra/clang-tidy/utils/LexerUtils.h
38 ↗(On Diff #220473)

Fair point, but putting an assertion or fixing the function would be good too.
But we really need unit tests to properly ensure the invariants.