[PATCH] D144708: [clangd] Fix UB in scanPreamble
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 24 01:44:40 PST 2023
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
kadircet marked an inline comment as done.
Closed by commit rGf393e1f6b3b4: [clangd] Fix UB in scanPreamble (authored by kadircet).
Changed prior to commit:
https://reviews.llvm.org/D144708?vs=500101&id=500112#toc
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D144708/new/
https://reviews.llvm.org/D144708
Files:
clang-tools-extra/clangd/Preamble.cpp
Index: clang-tools-extra/clangd/Preamble.cpp
===================================================================
--- clang-tools-extra/clangd/Preamble.cpp
+++ clang-tools-extra/clangd/Preamble.cpp
@@ -332,6 +332,8 @@
EmptyFS FS;
// Build and run Preprocessor over the preamble.
ParseInputs PI;
+ // Memory buffers below expect null-terminated && non-null strings. So make
+ // sure to always use PI.Contents!
PI.Contents = Contents.str();
PI.TFS = &FS;
PI.CompileCommand = Cmd;
@@ -345,8 +347,8 @@
// twice. However, it's important to precisely follow the preamble bounds used
// elsewhere.
auto Bounds = ComputePreambleBounds(*CI->getLangOpts(), *ContentsBuffer, 0);
- auto PreambleContents =
- llvm::MemoryBuffer::getMemBufferCopy(Contents.substr(0, Bounds.Size));
+ auto PreambleContents = llvm::MemoryBuffer::getMemBufferCopy(
+ llvm::StringRef(PI.Contents).take_front(Bounds.Size));
auto Clang = prepareCompilerInstance(
std::move(CI), nullptr, std::move(PreambleContents),
// Provide an empty FS to prevent preprocessor from performing IO. This
@@ -739,9 +741,8 @@
// whole preamble, which is terribly slow.
// - If scanning for Modified fails, cannot figure out newly added ones so
// there's nothing to do but generate an empty patch.
- auto BaselineScan = scanPreamble(
- // Contents needs to be null-terminated.
- Baseline.Preamble.getContents(), Modified.CompileCommand);
+ auto BaselineScan =
+ scanPreamble(Baseline.Preamble.getContents(), Modified.CompileCommand);
if (!BaselineScan) {
elog("Failed to scan baseline of {0}: {1}", FileName,
BaselineScan.takeError());
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144708.500112.patch
Type: text/x-patch
Size: 1687 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230224/3d0b39d1/attachment.bin>
More information about the cfe-commits
mailing list