From a8cbf21e5fd7fdbeb6fd1840bd78d5a688f32a90 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Tue, 4 Nov 2025 07:35:58 +0800 Subject: [PATCH 1/2] [clang-tidy][doc] add more information in twine-local's document explain more about use-after-free in llvm-twine-local add note about manually adjusting code after applying fix-it. fixed: #154810 --- .../clang-tidy/checks/llvm/twine-local.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/clang-tools-extra/docs/clang-tidy/checks/llvm/twine-local.rst b/clang-tools-extra/docs/clang-tidy/checks/llvm/twine-local.rst index ec9ef1c60913c..019fa973331dc 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/llvm/twine-local.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/llvm/twine-local.rst @@ -14,3 +14,21 @@ should be generally avoided. // becomes static std::string Moo = (Twine("bark") + "bah").str(); + +The ``llvm::Twine`` does not own the memory of its contents, so it is not +recommended to use ``Twine`` created from temporary strings or string literals. + +.. code-block:: c++ + + static Twine getModuleIdentifier(StringRef moduleName) { + return moduleName + "_module"; + } + void foo() { + Twine result = getModuleIdentifier(std::string{"abc"} + "def"); + // temporary std::string is destroyed here, result is dangling + } + +After applying this fix-it hints, the code will use ``std::string`` instead of +``Twine`` for local variables. However, ``llvm::Twine`` has lots of methods that +are incompatible with ``std::string``, so the user may need to adjust the code +manually after applying the fix-it hints. From 05a65097f05ca7babc2edc38a049e7dcdad16cd9 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Wed, 5 Nov 2025 19:23:02 +0800 Subject: [PATCH 2/2] fix --- clang-tools-extra/docs/clang-tidy/checks/llvm/twine-local.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/docs/clang-tidy/checks/llvm/twine-local.rst b/clang-tools-extra/docs/clang-tidy/checks/llvm/twine-local.rst index 019fa973331dc..6c994a48d83de 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/llvm/twine-local.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/llvm/twine-local.rst @@ -15,7 +15,7 @@ should be generally avoided. static std::string Moo = (Twine("bark") + "bah").str(); -The ``llvm::Twine`` does not own the memory of its contents, so it is not +The ``Twine`` does not own the memory of its contents, so it is not recommended to use ``Twine`` created from temporary strings or string literals. .. code-block:: c++ @@ -29,6 +29,6 @@ recommended to use ``Twine`` created from temporary strings or string literals. } After applying this fix-it hints, the code will use ``std::string`` instead of -``Twine`` for local variables. However, ``llvm::Twine`` has lots of methods that +``Twine`` for local variables. However, ``Twine`` has lots of methods that are incompatible with ``std::string``, so the user may need to adjust the code manually after applying the fix-it hints.