[clang] Fix clang crash when printing highlighted code in diagnostic (after #66514) (PR #80442)

via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 2 16:20:21 PST 2024


================
@@ -1349,7 +1349,7 @@ void TextDiagnostic::emitSnippetAndCaret(
   // Prepare source highlighting information for the lines we're about to
   // emit, starting from the first line.
   std::unique_ptr<SmallVector<StyleRange>[]> SourceStyles =
-      highlightLines(BufStart, Lines.first, Lines.second, PP, LangOpts,
+      highlightLines(BufData, Lines.first, Lines.second, PP, LangOpts,
----------------
alexfh wrote:

Some debugging.

Backtrace:
```
    frame #7: 0x00005555754b2ee6 clang`__assert_fail(assertion="CheckPoint >= Buff->getBufferStart() && CheckPoint <= Buff->getBufferEnd()", file="llvm-project/clang/lib/Frontend/TextDiagnostic.cpp", line=1157, function="std::unique_ptr<llvm::SmallVector<TextDiagnostic::StyleRange>[]> highlightLines(StringRef, unsigned int, unsigned int, const Preprocessor *, const LangOptions &, bool, FileID, const SourceManager &)") at logging.cc:57:3
    frame #8: 0x0000555567bfc020 clang`highlightLines(FileData=(Data = "\n clang diagnostic ignored \"-Wdeprecated-declarations\"\n", Length = 55), StartLineNumber=40, EndLineNumber=40, PP=0x000025c8bf806018, LangOpts=0x000025c8bf848918, ShowColors=true, FID=(ID = 23810), SM=0x000025c8bf892000) at TextDiagnostic.cpp:1156:5
    frame #9: 0x0000555567bfabcc clang`clang::TextDiagnostic::emitSnippetAndCaret(this=0x000025c8bfc62f00, Loc=FullSourceLoc @ 0x00007fffffff3b68, Level=Note, Ranges=0x00007fffffff3c80, Hints=ArrayRef @ 0x00007fffffff3b90) at TextDiagnostic.cpp:1352:7
    frame #10: 0x0000555567c021a7 clang`clang::TextDiagnostic::emitCodeContext(this=0x000025c8bfc62f00, Loc=FullSourceLoc @ 0x00007fffffff3c00, Level=Note, Ranges=0x00007fffffff3c80, Hints=ArrayRef @ 0x00007fffffff3c20) at TextDiagnostic.h:97:5
    frame #11: 0x0000555567bc1a9d clang`clang::DiagnosticRenderer::emitCaret(this=0x000025c8bfc62f00, Loc=FullSourceLoc @ 0x00007fffffff3ce0, Level=Note, Ranges=ArrayRef @ 0x00007fffffff3cd0, Hints=ArrayRef @ 0x00007fffffff3d00) at DiagnosticRenderer.cpp:429:3
    frame #12: 0x0000555567bc1112 clang`clang::DiagnosticRenderer::emitDiagnostic(this=0x000025c8bfc62f00, Loc=FullSourceLoc @ 0x00007fffffff4270, Level=Note, Message=(Data = "expanded from hereo '_GLIB_GNUC_DO_PRAGMA'_FOR'_48_FOR'", Length = 18), Ranges=ArrayRef @ 0x00007fffffff42a0, FixItHints=ArrayRef @ 0x00007fffffff42b0, D=clang::DiagOrStoredDiag @ 0x00007fffffff4258) at DiagnosticRenderer.cpp:127:5
    frame #13: 0x0000555567bc3549 clang`clang::DiagnosticRenderer::emitSingleMacroExpansion(this=0x000025c8bfc62f00, Loc=FullSourceLoc @ 0x00007fffffff44c8, Level=Warning, Ranges=ArrayRef @ 0x00007fffffff44b8) at DiagnosticRenderer.cpp:454:3
    frame #14: 0x0000555567bc1f4b clang`clang::DiagnosticRenderer::emitMacroExpansions(this=0x000025c8bfc62f00, Loc=FullSourceLoc @ 0x00007fffffff4840, Level=Warning, Ranges=ArrayRef @ 0x00007fffffff4830, Hints=ArrayRef @ 0x00007fffffff4860) at DiagnosticRenderer.cpp:569:7
    frame #15: 0x0000555567bc11dc clang`clang::DiagnosticRenderer::emitDiagnostic(this=0x000025c8bfc62f00, Loc=FullSourceLoc @ 0x00007fffffff4dd0, Level=Warning, Message=(Data = "Deprecated pre-processor symbol: replace with \"static inline\" [-W#pragma-messages]\xff\xff\xff\U0000007f", Length = 82), Ranges=ArrayRef @ 0x00007fffffff4e00, FixItHints=ArrayRef @ 0x00007fffffff4e10, D=clang::DiagOrStoredDiag @ 0x00007fffffff4db8) at DiagnosticRenderer.cpp:132:7
    frame #16: 0x0000555567bf73b6 clang`clang::TextDiagnosticPrinter::HandleDiagnostic(this=0x000025c8bfc16630, Level=Warning, Info=0x00007fffffff50c8) at TextDiagnosticPrinter.cpp:151:13
    frame #17: 0x000055556c6a0148 clang`clang::DiagnosticIDs::EmitDiag(this=0x000025c8bfc04840, Diag=0x000025c8bf85c900, DiagLevel=Warning) const at DiagnosticIDs.cpp:823:16
    frame #18: 0x000055556c69fef0 clang`clang::DiagnosticIDs::ProcessDiag(this=0x000025c8bfc04840, Diag=0x000025c8bf85c900) const at DiagnosticIDs.cpp:815:3
    frame #19: 0x000055556c69437a clang`clang::DiagnosticsEngine::ProcessDiag(this=0x000025c8bf85c900) at Diagnostic.h:1042:19
    frame #20: 0x000055556c68ef05 clang`clang::DiagnosticsEngine::EmitCurrentDiagnostic(this=0x000025c8bf85c900, Force=false) at Diagnostic.cpp:545:15
    frame #21: 0x0000555562f61b4d clang`clang::DiagnosticBuilder::Emit(this=0x00007fffffff5420) at Diagnostic.h:1325:28
    frame #22: 0x0000555562f5fb6c clang`clang::DiagnosticBuilder::~DiagnosticBuilder(this=0x00007fffffff5420) at Diagnostic.h:1366:26
    frame #23: 0x000055556c5db9c0 clang`(anonymous namespace)::PragmaMessageHandler::HandlePragma(this=0x000025c8bfc62c40, PP=0x000025c8bf806018, Introducer=PragmaIntroducer @ 0x00007fffffff54f8, Tok=0x00007fffffff5720) at Pragma.cpp:1675:5
    frame #24: 0x000055556c5d33fb clang`clang::PragmaNamespace::HandlePragma(this=0x000025c8bfc636c0, PP=0x000025c8bf806018, Introducer=PragmaIntroducer @ 0x00007fffffff55c8, Tok=0x00007fffffff5720) at Pragma.cpp:124:12
    frame #25: 0x000055556c5d33fb clang`clang::PragmaNamespace::HandlePragma(this=0x000025c8bfc63600, PP=0x000025c8bf806018, Introducer=PragmaIntroducer @ 0x00007fffffff5698, Tok=0x00007fffffff5720) at Pragma.cpp:124:12
    frame #26: 0x000055556c5d3557 clang`clang::Preprocessor::HandlePragmaDirective(this=0x000025c8bf806018, Introducer=PragmaIntroducer @ 0x00007fffffff5748) at Pragma.cpp:178:19
    frame #27: 0x000055556c5d3fdf clang`clang::Preprocessor::Handle_Pragma(this=0x000025c8bf806018, Tok=0x000025c8bf9c2010) at Pragma.cpp:300:3
    frame #28: 0x000055556c5b1892 clang`clang::Preprocessor::ExpandBuiltinMacro(this=0x000025c8bf806018, Tok=0x000025c8bf9c2010) at PPMacroExpansion.cpp:1506:12
    frame #29: 0x000055556c5b0af3 clang`clang::Preprocessor::HandleMacroExpandedIdentifier(this=0x000025c8bf806018, Identifier=0x000025c8bf9c2010, M=0x00007fffffff6da0) at PPMacroExpansion.cpp:493:5
    frame #30: 0x000055556c5fe271 clang`clang::Preprocessor::HandleIdentifier(this=0x000025c8bf806018, Identifier=0x000025c8bf9c2010) at Preprocessor.cpp:815:18
    frame #31: 0x000055556c6169f7 clang`clang::TokenLexer::Lex(this=0x000025c8bfc16a20, Tok=0x000025c8bf9c2010) at TokenLexer.cpp:717:17
    frame #32: 0x000055556c5a55dd clang`clang::Preprocessor::CLK_TokenLexer(P=0x000025c8bf806018, Result=0x000025c8bf9c2010) at Preprocessor.h:2926:29
    frame #33: 0x000055556c5fe790 clang`clang::Preprocessor::Lex(this=0x000025c8bf806018, Result=0x000025c8bf9c2010) at Preprocessor.cpp:872:11
    frame #34: 0x0000555567eccf2b clang`clang::Parser::ConsumeToken(this=0x000025c8bf9c2000) at Parser.h:518:8
    frame #35: 0x0000555567eccb54 clang`clang::Parser::ConsumeAnyToken(this=0x000025c8bf9c2000, ConsumeCodeCompletionTok=false) at Parser.h:556:12
    frame #36: 0x0000555567fc1d93 clang`clang::Parser::ParseDeclarationSpecifiers(this=0x000025c8bf9c2000, DS=0x00007fffffff9188, TemplateInfo=0x00007fffffff90c0, AS=AS_none, DSContext=DSC_top_level, LateAttrs=0x0000000000000000, AllowImplicitTypename=Yes) at ParseDecl.cpp:4667:7
```

```
frame #8: 0x0000555567bfc020 clang`highlightLines(FileData=(Data = "\n clang diagnostic ignored \"-Wdeprecated-declarations\"\n", Length = 55), StartLineNumber=40, EndLineNumber=40, PP=0x000025c8bf806018, LangOpts=0x000025c8bf848918, ShowColors=true, FID=(ID = 23810), SM=0x000025c8bf892000) at TextDiagnostic.cpp:1156:5
   1153       FileData.data() +
   1154       SM.getDecomposedLoc(SM.translateLineCol(FID, StartLineNumber, 1)).second;
   1155   if (const char *CheckPoint = PP->getCheckPoint(FID, FirstLineStart)) {
-> 1156     assert(CheckPoint >= Buff->getBufferStart() &&
   1157            CheckPoint <= Buff->getBufferEnd());
   1158     assert(CheckPoint <= FirstLineStart);
   1159     size_t Offset = CheckPoint - Buff->getBufferStart();
(lldb) p Buff->BufferEnd
(const char *) 0x000025c8bf052567 ""
(lldb) p CheckPoint
(const char *) 0x000025c8bf05284a "\n"
(lldb) x/1024b Buff->BufferStart
0x25c8bf052530: \n clang diagnostic ignored "-Wde
0x25c8bf052550: precated-declarations"\n\0\nglib_au
0x25c8bf052570: toptr_clear_GUri\0\nglib_autoptr_c
0x25c8bf052590: leanup_GUri\0\nglib_autoptr_clear_
0x25c8bf0525b0: GUri\0\nglib_listautoptr_cleanup_G
0x25c8bf0525d0: Uri\0\nglib_slistautoptr_cleanup_G
0x25c8bf0525f0: Uri\0\nglib_queueautoptr_cleanup_G
0x25c8bf052610: Uri\0\n clang diagnostic pop\n\0\nGPa
0x25c8bf052630: thBuf_autoptr\0\nGPathBuf_listauto
0x25c8bf052650: ptr\0\nGPathBuf_slistautoptr\0\nGPat
0x25c8bf052670: hBuf_queueautoptr\0\n clang diagno
0x25c8bf052690: stic push\n\0\n clang diagnostic ig
0x25c8bf0526b0: nored "-Wdeprecated-declarations
0x25c8bf0526d0: "\n\0\nglib_autoptr_clear_GPathBuf\0
0x25c8bf0526f0: \nglib_autoptr_cleanup_GPathBuf\0\n
0x25c8bf052710: glib_autoptr_clear_GPathBuf\0\ngli
0x25c8bf052730: b_listautoptr_cleanup_GPathBuf\0\n
0x25c8bf052750: glib_slistautoptr_cleanup_GPathB
0x25c8bf052770: uf\0\nglib_queueautoptr_cleanup_GP
0x25c8bf052790: athBuf\0\n clang diagnostic pop\n\0\n
0x25c8bf0527b0:  clang diagnostic push\n\0\n clang
0x25c8bf0527d0: diagnostic ignored "-Wdeprecated
0x25c8bf0527f0: -declarations"\n\0\nglib_auto_clean
0x25c8bf052810: up_GPathBuf\0\n clang diagnostic p
0x25c8bf052830: op\n\0\n clang diagnostic pop\n\0\n"st
0x25c8bf052850: atic inline"\0\n"Deprecated pre-pr
0x25c8bf052870: ocessor symbol: replace with \"s
0x25c8bf052890: tatic inline\""\0\n"GCC warning \"
0x25c8bf0528b0: Deprecated pre-processor symbol:
0x25c8bf0528d0:  replace with \\\"static inline\
0x25c8bf0528f0: \\"\""\0\n GCC warning "Deprecated
0x25c8bf052910:  pre-processor symbol: replace w
```

So the buffer is inside the scratch space, which naturally contains \0's. And the problem is when the code to highlight is anywhere beyond the first \0 character in this buffer - then CheckPoint will be outside of the StringRef created from just the pointer to the buffer start.

https://github.com/llvm/llvm-project/pull/80442


More information about the cfe-commits mailing list