[clang] [clang][Diagnostics] Highlight code snippets (PR #66514)

via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 2 06:51:38 PST 2024


eaeltsin wrote:

Ok, the problem is in clang/lib/Frontend/TextDiagnostic.cpp:1352:
```
  std::unique_ptr<SmallVector<StyleRange>[]> SourceStyles =
      highlightLines(BufStart, Lines.first, Lines.second, PP, LangOpts,
                     DiagOpts->ShowColors, FID, SM);
```

Looks like passing `BufStart` creates a StringRef that doesn't cover all the data.

Replacing this to 
```
  std::unique_ptr<SmallVector<StyleRange>[]> SourceStyles =
      highlightLines(StringRef(BufStart, BufEnd - BufStart), Lines.first, Lines.second, PP, LangOpts,
                     DiagOpts->ShowColors, FID, SM);
```
makes the crash go away.

I don't know this code so cannot comment more on what actually happens. The only thing to add is that the crash occurred because offset returned in line 1154 was way out of bounds of buffer created at line 1148.

Please fix.


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


More information about the cfe-commits mailing list