[PATCH] D122391: [llvm] Fix string copy confusion

Nathan Sidwell via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 24 06:01:33 PDT 2022


urnathan created this revision.
urnathan added reviewers: dblaikie, erichkeane, bruno.
Herald added a subscriber: hiraditya.
Herald added a project: All.
urnathan requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The microsoft demangler makes copies of the demangled strings, but has some confusion between StringView representation (sans NUL), and C-strings (with NUL).  Here we also have a use of strcpy, which happens to work because the incoming string view happens to have a trailing NUL.  But a simple memcpy excluding the NUL is sufficient.

This is broken out of D120990 <https://reviews.llvm.org/D120990> and is a prerequisite of that patch.


https://reviews.llvm.org/D122391

Files:
  llvm/lib/Demangle/MicrosoftDemangle.cpp


Index: llvm/lib/Demangle/MicrosoftDemangle.cpp
===================================================================
--- llvm/lib/Demangle/MicrosoftDemangle.cpp
+++ llvm/lib/Demangle/MicrosoftDemangle.cpp
@@ -245,8 +245,8 @@
 }
 
 StringView Demangler::copyString(StringView Borrowed) {
-  char *Stable = Arena.allocUnalignedBuffer(Borrowed.size() + 1);
-  std::strcpy(Stable, Borrowed.begin());
+  char *Stable = Arena.allocUnalignedBuffer(Borrowed.size());
+  std::memcpy(Stable, Borrowed.begin(), Borrowed.size());
 
   return {Stable, Borrowed.size()};
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122391.417897.patch
Type: text/x-patch
Size: 558 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220324/c1157683/attachment.bin>


More information about the llvm-commits mailing list