From: Haojian Wu Date: Fri, 15 Jul 2022 13:12:46 +0000 (+0200) Subject: [pseudo] Share the underly payload when stripping comments for a token stream X-Git-Tag: upstream/15.0.7~1573 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=76910d4a56c8dba000f198bba13e71cf0492c8cb;p=platform%2Fupstream%2Fllvm.git [pseudo] Share the underly payload when stripping comments for a token stream `stripComments(cook(...))` is a common pattern being written. Without this patch, this has a use-after-free issue (cook returns a temporary TokenStream object which has its own payload, but the payload is not shared with the one returned by stripComments). Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D125311 --- diff --git a/clang-tools-extra/pseudo/include/clang-pseudo/Token.h b/clang-tools-extra/pseudo/include/clang-pseudo/Token.h index b558891..36e5221 100644 --- a/clang-tools-extra/pseudo/include/clang-pseudo/Token.h +++ b/clang-tools-extra/pseudo/include/clang-pseudo/Token.h @@ -170,6 +170,18 @@ public: return Storage[1]; } + /// Returns the shared payload. + std::shared_ptr getPayload() const { return Payload; } + /// Adds the given payload to the stream. + void addPayload(std::shared_ptr P) { + if (!Payload) + Payload = std::move(P); + else + Payload = std::make_shared< + std::pair, std::shared_ptr>>( + std::move(P), std::move(Payload)); + } + /// Print the tokens in this stream to the output stream. /// /// The presence of newlines/spaces is preserved, but not the quantity. diff --git a/clang-tools-extra/pseudo/lib/Lex.cpp b/clang-tools-extra/pseudo/lib/Lex.cpp index 6a5a10f..c96e2f27 100644 --- a/clang-tools-extra/pseudo/lib/Lex.cpp +++ b/clang-tools-extra/pseudo/lib/Lex.cpp @@ -77,7 +77,7 @@ TokenStream cook(const TokenStream &Code, const LangOptions &LangOpts) { auto CleanedStorage = std::make_shared(); clang::IdentifierTable Identifiers(LangOpts); TokenStream Result(CleanedStorage); - + Result.addPayload(Code.getPayload()); for (auto Tok : Code.tokens()) { if (Tok.flag(LexFlags::NeedsCleaning)) { // Remove escaped newlines and trigraphs. diff --git a/clang-tools-extra/pseudo/lib/Token.cpp b/clang-tools-extra/pseudo/lib/Token.cpp index b58c8e4..5b07a62 100644 --- a/clang-tools-extra/pseudo/lib/Token.cpp +++ b/clang-tools-extra/pseudo/lib/Token.cpp @@ -116,7 +116,7 @@ clang::LangOptions genericLangOpts(clang::Language Lang, } TokenStream stripComments(const TokenStream &Input) { - TokenStream Out; + TokenStream Out(Input.getPayload()); for (const Token &T : Input.tokens()) { if (T.Kind == tok::comment) continue;