[llvm] 8603552 - [MC] AsmLexer assert buffer is null-terminated at CurBuf.end() (#154972)

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 18 02:09:46 PST 2025


Author: Szymon Piotr Milczek
Date: 2025-11-18T11:09:42+01:00
New Revision: 8603552133c832080dac6de2460ebf5d2a1f1be0

URL: https://github.com/llvm/llvm-project/commit/8603552133c832080dac6de2460ebf5d2a1f1be0
DIFF: https://github.com/llvm/llvm-project/commit/8603552133c832080dac6de2460ebf5d2a1f1be0.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCParser/AsmLexer.h
    llvm/lib/MC/MCParser/AsmLexer.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCParser/AsmLexer.h b/llvm/include/llvm/MC/MCParser/AsmLexer.h
index c514b768637d1..7848fc706d5eb 100644
--- a/llvm/include/llvm/MC/MCParser/AsmLexer.h
+++ b/llvm/include/llvm/MC/MCParser/AsmLexer.h
@@ -44,6 +44,7 @@ class AsmLexer {
   SmallVector<AsmToken, 1> 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
@@ -190,6 +191,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 1af4a297babaa..8e4b7be98bdb6 100644
--- a/llvm/lib/MC/MCParser/AsmLexer.cpp
+++ b/llvm/lib/MC/MCParser/AsmLexer.cpp
@@ -119,6 +119,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)


        


More information about the llvm-commits mailing list