This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Fix UB in scanPreamble
ClosedPublic

Authored by kadircet on Feb 24 2023, 12:53 AM.

Details

Summary

getMemBufferCopy triggers an UB when it receives a default constructed
StringRef. Make sure that we're always passing the null-terminated string
created in ParseInputs throughout the scanPreamble.

Diff Detail

Event Timeline

kadircet created this revision.Feb 24 2023, 12:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 24 2023, 12:53 AM
Herald added a subscriber: arphaman. · View Herald Transcript
kadircet requested review of this revision.Feb 24 2023, 12:53 AM
hokein accepted this revision.Feb 24 2023, 1:12 AM
hokein added inline comments.
clang-tools-extra/clangd/Preamble.cpp
350–351

Looks like we have an extra cost here -- the std::string.substr will construct a new string everytime, we could save it by using llvm::StringRef(PI.Contents).substr(0, Bounds.Size).

This revision is now accepted and ready to land.Feb 24 2023, 1:12 AM
kadircet marked an inline comment as done.Feb 24 2023, 1:38 AM
kadircet added inline comments.
clang-tools-extra/clangd/Preamble.cpp
350–351

thanks!

This revision was landed with ongoing or failed builds.Feb 24 2023, 1:44 AM
This revision was automatically updated to reflect the committed changes.
kadircet marked an inline comment as done.