This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Fix a nullptr-dereference crash in computeIncludeCleanerFindings.
ClosedPublic

Authored by hokein on Apr 6 2023, 1:14 AM.

Details

Summary

Be more robust, we shuold not crash when we cannot find the corresponding token from the
tokenbuffer.

Diff Detail

Event Timeline

hokein created this revision.Apr 6 2023, 1:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 6 2023, 1:14 AM
Herald added a subscriber: arphaman. · View Herald Transcript
hokein requested review of this revision.Apr 6 2023, 1:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 6 2023, 1:14 AM
hokein updated this revision to Diff 511319.Apr 6 2023, 1:15 AM

add a comment.

could you give some more details about the crash, so that we actually now where to fix it? e.g. is it token buffers missing the spelled token at that offset somehow ? or is it AST reporting a weird source locations for user-defined literals and we need to do some sort of post processing (or maybe it should've been filtered all together). as doing it like this will imply us completely eating such diagnostics.

hokein added a comment.Apr 6 2023, 5:39 AM

could you give some more details about the crash, so that we actually now where to fix it? e.g. is it token buffers missing the spelled token at that offset somehow ? or is it AST reporting a weird source locations for user-defined literals and we need to do some sort of post processing (or maybe it should've been filtered all together). as doing it like this will imply us completely eating such diagnostics.

sorry, for not giving more details. See the other comment.

clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
421

here is the UserDefinedLiteral AST node:

`-UserDefinedLiteral 0x5556682e4500 <line:46:3> 'int'
      |-ImplicitCastExpr 0x5556682e44e8 <col:4> 'int (*)(unsigned long long)' <FunctionToPointerDecay>
      | `-DeclRefExpr 0x5556682e44a0 <col:4> 'int (unsigned long long)' lvalue Function 0x5556682e4290 'operator""s' 'int (unsigned long long)'
      `-IntegerLiteral 0x5556682e4480 <col:3> 'unsigned long long' 1

The source location of the UserDefinedLiteral points to ^1s, while the source location of the inner DeclRefExpr points to 1^s (1 offset shift). In the token buffer, we only have a single spelled token 1s, thus AST.getTokens().spelledTokenAt can not find any token that starts at 1^s source loc.

429

T

kadircet added inline comments.Apr 13 2023, 12:03 AM
clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
421

thanks, that makes sense!

then instead of "suppressing" these, can we use spelledTokensTouching().front() (and asserting that it isn't empty?)

hokein updated this revision to Diff 513134.Apr 13 2023, 2:39 AM
hokein marked an inline comment as not done.

use syntax::spelledTokensTouching.

clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
421

Thanks, this is a good idea.

But we have to use the back(), for the case ns::^abc (we want to point to abc, not the ::).

kadircet accepted this revision.Apr 13 2023, 3:07 AM
This revision is now accepted and ready to land.Apr 13 2023, 3:07 AM
This revision was landed with ongoing or failed builds.Apr 13 2023, 3:17 AM
This revision was automatically updated to reflect the committed changes.