[Demangle] replace std::string_view::substr which may throw
authorNick Desaulniers <ndesaulniers@google.com>
Fri, 21 Apr 2023 22:51:29 +0000 (15:51 -0700)
committerNick Desaulniers <ndesaulniers@google.com>
Fri, 21 Apr 2023 22:56:55 +0000 (15:56 -0700)
llvm/Demangle copies the implementation from libcxxabi/src/demangle/.
libcxxabi/ cannot use potentially-throwing std::string_view::substr, so
change llvm/Demangle to avoid these function calls.

I ran into linkage failures stemming from the usage of
std::string_view::substr. substr does a bounds check and may throw.

Fixes: f198e0b594aa ("[StringView] remove dropFront")

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D148959

llvm/include/llvm/Demangle/ItaniumDemangle.h

index 07b1097..428f064 100644 (file)
@@ -1842,7 +1842,7 @@ public:
       OB += "0";
     } else if (Offset[0] == 'n') {
       OB += "-";
-      OB += Offset.substr(1);
+      OB += std::string_view(Offset.data() + 1, Offset.size() - 1);
     } else {
       OB += Offset;
     }
@@ -2270,7 +2270,7 @@ public:
     OB.printClose();
 
     if (Integer[0] == 'n')
-      OB << '-' << Integer.substr(1);
+      OB << '-' << std::string_view(Integer.data() + 1, Integer.size() - 1);
     else
       OB << Integer;
   }
@@ -2294,7 +2294,7 @@ public:
     }
 
     if (Value[0] == 'n')
-      OB << '-' << Value.substr(1);
+      OB << '-' << std::string_view(Value.data() + 1, Value.size() - 1);
     else
       OB += Value;
 
@@ -3713,7 +3713,8 @@ Node *AbstractManglingParser<Derived, Alloc>::parseQualifiedType() {
 
     // extension            ::= U <objc-name> <objc-type>  # objc-type<identifier>
     if (llvm::itanium_demangle::starts_with(Qual, "objcproto")) {
-      std::string_view ProtoSourceName = Qual.substr(std::strlen("objcproto"));
+      constexpr size_t Len = sizeof("objcproto") - 1;
+      std::string_view ProtoSourceName(Qual.data() + Len, Qual.size() - Len);
       std::string_view Proto;
       {
         ScopedOverride<const char *> SaveFirst(First, &*ProtoSourceName.begin()),