diff --git a/llvm/include/llvm/MC/MCParser/AsmLexer.h b/llvm/include/llvm/MC/MCParser/AsmLexer.h index 11d32fbb64702..e4cdb2ccd2e6e 100644 --- a/llvm/include/llvm/MC/MCParser/AsmLexer.h +++ b/llvm/include/llvm/MC/MCParser/AsmLexer.h @@ -45,6 +45,7 @@ class AsmLexer { SmallVector CurTok; const char *CurPtr = nullptr; + /// NULL-terminated buffer. NULL terminator must reside at `CurBuf.end()`. StringRef CurBuf; /// The location and description of the current error @@ -191,6 +192,12 @@ class AsmLexer { /// literals. void setLexHLASMStrings(bool V) { LexHLASMStrings = V; } + /// Set buffer to be lexed. + /// `Buf` must be NULL-terminated. NULL terminator must reside at `Buf.end()`. + /// `ptr` if provided must be in range [`Buf.begin()`, `buf.end()`] or NULL. + /// Specifies where lexing of buffer should begin. + /// `EndStatementAtEOF` specifies whether `AsmToken::EndOfStatement` should be + /// returned upon reaching end of buffer. LLVM_ABI void setBuffer(StringRef Buf, const char *ptr = nullptr, bool EndStatementAtEOF = true); diff --git a/llvm/lib/MC/MCParser/AsmLexer.cpp b/llvm/lib/MC/MCParser/AsmLexer.cpp index 968ccf776440b..6a89c1d09af09 100644 --- a/llvm/lib/MC/MCParser/AsmLexer.cpp +++ b/llvm/lib/MC/MCParser/AsmLexer.cpp @@ -120,6 +120,11 @@ AsmLexer::AsmLexer(const MCAsmInfo &MAI) : MAI(MAI) { void AsmLexer::setBuffer(StringRef Buf, const char *ptr, bool EndStatementAtEOF) { + // Buffer must be NULL-terminated. NULL terminator must reside at `Buf.end()`. + // It must be safe to dereference `Buf.end()`. + assert(*Buf.end() == '\0' && + "Buffer provided to AsmLexer lacks null terminator."); + CurBuf = Buf; if (ptr)