Skip to content

Commit 8603552

Browse files
authored
[MC] AsmLexer assert buffer is null-terminated at CurBuf.end() (#154972)
AsmLexer expects the buffer it's provided for lexing to be NULL-terminated, where the NULL terminator is pointed to by `CurBuf.end()`. However, this expectation isn't explicitly stated anywhere. This commit adds a couple of comments as well as an assert as means of documenting this expectation.
1 parent 542d88d commit 8603552

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

llvm/include/llvm/MC/MCParser/AsmLexer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class AsmLexer {
4444
SmallVector<AsmToken, 1> CurTok;
4545

4646
const char *CurPtr = nullptr;
47+
/// NULL-terminated buffer. NULL terminator must reside at `CurBuf.end()`.
4748
StringRef CurBuf;
4849

4950
/// The location and description of the current error
@@ -190,6 +191,12 @@ class AsmLexer {
190191
/// literals.
191192
void setLexHLASMStrings(bool V) { LexHLASMStrings = V; }
192193

194+
/// Set buffer to be lexed.
195+
/// `Buf` must be NULL-terminated. NULL terminator must reside at `Buf.end()`.
196+
/// `ptr` if provided must be in range [`Buf.begin()`, `buf.end()`] or NULL.
197+
/// Specifies where lexing of buffer should begin.
198+
/// `EndStatementAtEOF` specifies whether `AsmToken::EndOfStatement` should be
199+
/// returned upon reaching end of buffer.
193200
LLVM_ABI void setBuffer(StringRef Buf, const char *ptr = nullptr,
194201
bool EndStatementAtEOF = true);
195202

llvm/lib/MC/MCParser/AsmLexer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ AsmLexer::AsmLexer(const MCAsmInfo &MAI) : MAI(MAI) {
119119

120120
void AsmLexer::setBuffer(StringRef Buf, const char *ptr,
121121
bool EndStatementAtEOF) {
122+
// Buffer must be NULL-terminated. NULL terminator must reside at `Buf.end()`.
123+
// It must be safe to dereference `Buf.end()`.
124+
assert(*Buf.end() == '\0' &&
125+
"Buffer provided to AsmLexer lacks null terminator.");
126+
122127
CurBuf = Buf;
123128

124129
if (ptr)

0 commit comments

Comments
 (0)