diff --git a/llvm/include/llvm/Demangle/ItaniumDemangle.h b/llvm/include/llvm/Demangle/ItaniumDemangle.h --- a/llvm/include/llvm/Demangle/ItaniumDemangle.h +++ b/llvm/include/llvm/Demangle/ItaniumDemangle.h @@ -1583,7 +1583,8 @@ if (isInstantiation()) { // The instantiations are typedefs that drop the "basic_" prefix. assert(SV.startsWith("basic_")); - SV = SV.dropFront(sizeof("basic_") - 1); + assert(sizeof("basic_") - 1 <= SV.size()); + SV = SV.substr(sizeof("basic_") - 1); } return SV; } @@ -1838,7 +1839,8 @@ OB += "0"; } else if (Offset[0] == 'n') { OB += "-"; - OB += Offset.dropFront(); + assert(1 <= Offset.size()); + OB += Offset.substr(1); } else { OB += Offset; } @@ -2263,9 +2265,10 @@ Ty->print(OB); OB.printClose(); - if (Integer[0] == 'n') - OB << "-" << Integer.dropFront(1); - else + if (Integer[0] == 'n') { + assert(1 <= Integer.size()); + OB << "-" << Integer.substr(1); + } else OB << Integer; } }; @@ -2289,7 +2292,8 @@ if (Value[0] == 'n') { OB += '-'; - OB += Value.dropFront(1); + assert(1 <= Value.size()); + OB += Value.substr(1); } else OB += Value; @@ -2632,7 +2636,8 @@ if (Kind < Unnameable) { assert(Res.startsWith("operator") && "operator name does not start with 'operator'"); - Res = Res.dropFront(sizeof("operator") - 1); + assert(sizeof("operator") - 1 <= Res.size()); + Res = Res.substr(sizeof("operator") - 1); Res.consumeFront(' '); } return Res; @@ -3706,7 +3711,8 @@ // extension ::= U # objc-type if (Qual.startsWith("objcproto")) { - StringView ProtoSourceName = Qual.dropFront(std::strlen("objcproto")); + assert(std::strlen("objcproto") <= Qual.size()); + StringView ProtoSourceName = Qual.substr(std::strlen("objcproto")); StringView Proto; { ScopedOverride SaveFirst(First, ProtoSourceName.begin()), diff --git a/llvm/include/llvm/Demangle/StringView.h b/llvm/include/llvm/Demangle/StringView.h --- a/llvm/include/llvm/Demangle/StringView.h +++ b/llvm/include/llvm/Demangle/StringView.h @@ -55,12 +55,6 @@ return npos; } - StringView dropFront(size_t N = 1) const { - if (N >= size()) - N = size(); - return StringView(First + N, Last); - } - StringView dropBack(size_t N = 1) const { if (N >= size()) N = size(); @@ -85,14 +79,16 @@ bool consumeFront(char C) { if (!startsWith(C)) return false; - *this = dropFront(1); + assert(1 <= size()); + *this = substr(1); return true; } bool consumeFront(StringView S) { if (!startsWith(S)) return false; - *this = dropFront(S.size()); + assert(S.size() <= size()); + *this = substr(S.size()); return true; } diff --git a/llvm/lib/Demangle/MicrosoftDemangle.cpp b/llvm/lib/Demangle/MicrosoftDemangle.cpp --- a/llvm/lib/Demangle/MicrosoftDemangle.cpp +++ b/llvm/lib/Demangle/MicrosoftDemangle.cpp @@ -168,11 +168,13 @@ // ambiguity is also why single digit encoded numbers use 0-9 rather than A-J. if (Candidate[0] < 'B' || Candidate[0] > 'P') return false; - Candidate = Candidate.dropFront(); + assert(1 <= Candidate.size()); + Candidate = Candidate.substr(1); while (!Candidate.empty()) { if (Candidate[0] < 'A' || Candidate[0] > 'P') return false; - Candidate = Candidate.dropFront(); + assert(1 <= Candidate.size()); + Candidate = Candidate.substr(1); } return true; @@ -486,7 +488,8 @@ IdentifierNode * Demangler::demangleFunctionIdentifierCode(StringView &MangledName) { assert(MangledName.startsWith('?')); - MangledName = MangledName.dropFront(); + assert(1 <= MangledName.size()); + MangledName = MangledName.substr(1); if (MangledName.empty()) { Error = true; return nullptr; @@ -765,7 +768,8 @@ return nullptr; } const char *Start = MangledName.begin(); - MangledName = MangledName.dropFront(MD5Last + 1); + assert(MD5Last + 1 <= MangledName.size()); + MangledName = MangledName.substr(MD5Last + 1); // There are two additional special cases for MD5 names: // 1. For complete object locators where the object name is long enough @@ -902,7 +906,8 @@ if (startsWithDigit(MangledName)) { uint64_t Ret = MangledName[0] - '0' + 1; - MangledName = MangledName.dropFront(1); + assert(1 <= MangledName.size()); + MangledName = MangledName.substr(1); return {Ret, IsNegative}; } @@ -910,7 +915,8 @@ for (size_t i = 0; i < MangledName.size(); ++i) { char C = MangledName[i]; if (C == '@') { - MangledName = MangledName.dropFront(i + 1); + assert(i + 1 <= MangledName.size()); + MangledName = MangledName.substr(i + 1); return {Ret, IsNegative}; } if ('A' <= C && C <= 'P') { @@ -965,7 +971,8 @@ return nullptr; } - MangledName = MangledName.dropFront(); + assert(1 <= MangledName.size()); + MangledName = MangledName.substr(1); return Backrefs.Names[I]; } @@ -1036,7 +1043,8 @@ if (!MangledName.startsWith('?')) return MangledName.popFront(); - MangledName = MangledName.dropFront(); + assert(1 <= MangledName.size()); + MangledName = MangledName.substr(1); if (MangledName.empty()) goto CharLiteralError; @@ -1050,14 +1058,16 @@ // Don't append the null terminator. uint8_t C1 = rebasedHexDigitToNumber(Nibbles[0]); uint8_t C2 = rebasedHexDigitToNumber(Nibbles[1]); - MangledName = MangledName.dropFront(2); + assert(2 <= MangledName.size()); + MangledName = MangledName.substr(2); return (C1 << 4) | C2; } if (startsWithDigit(MangledName)) { const char *Lookup = ",/\\:. \n\t'-"; char C = Lookup[MangledName[0] - '0']; - MangledName = MangledName.dropFront(); + assert(1 <= MangledName.size()); + MangledName = MangledName.substr(1); return C; } @@ -1067,7 +1077,8 @@ '\xEF', '\xF0', '\xF1', '\xF2', '\xF3', '\xF4', '\xF5', '\xF6', '\xF7', '\xF8', '\xF9', '\xFA'}; char C = Lookup[MangledName[0] - 'a']; - MangledName = MangledName.dropFront(); + assert(1 <= MangledName.size()); + MangledName = MangledName.substr(1); return C; } @@ -1077,7 +1088,8 @@ '\xCF', '\xD0', '\xD1', '\xD2', '\xD3', '\xD4', '\xD5', '\xD6', '\xD7', '\xD8', '\xD9', '\xDA'}; char C = Lookup[MangledName[0] - 'A']; - MangledName = MangledName.dropFront(); + assert(1 <= MangledName.size()); + MangledName = MangledName.substr(1); return C; } @@ -1310,7 +1322,8 @@ if (CrcEndPos == StringView::npos) goto StringLiteralError; CRC = MangledName.substr(0, CrcEndPos); - MangledName = MangledName.dropFront(CrcEndPos + 1); + assert(CrcEndPos + 1 <= MangledName.size()); + MangledName = MangledName.substr(CrcEndPos + 1); if (MangledName.empty()) goto StringLiteralError; @@ -1391,7 +1404,8 @@ if (i == 0) break; S = MangledName.substr(0, i); - MangledName = MangledName.dropFront(i + 1); + assert(i + 1 <= MangledName.size()); + MangledName = MangledName.substr(i + 1); if (Memorize) memorizeString(S); @@ -1416,6 +1430,7 @@ } StringView NamespaceKey = MangledName.substr(0, EndPos); memorizeString(NamespaceKey); + assert(EndPos + 1 <= MangledName.size()); MangledName = MangledName.substr(EndPos + 1); return Node; } @@ -2124,7 +2139,8 @@ Error = true; return nullptr; } - MangledName = MangledName.dropFront(); + assert(1 <= MangledName.size()); + MangledName = MangledName.substr(1); *Current = Arena.alloc(); (*Current)->N = Backrefs.FunctionParams[N]; @@ -2206,7 +2222,8 @@ TP.N = TPRN = Arena.alloc(); TPRN->IsMemberPointer = true; - MangledName = MangledName.dropFront(); + assert(1 <= MangledName.size()); + MangledName = MangledName.substr(1); // 1 - single inheritance // H - multiple inheritance // I - virtual inheritance @@ -2252,7 +2269,8 @@ TP.N = TPRN = Arena.alloc(); // Data member pointer. - MangledName = MangledName.dropFront(); + assert(1 <= MangledName.size()); + MangledName = MangledName.substr(1); char InheritanceSpecifier = MangledName.popFront(); switch (InheritanceSpecifier) {