[llvm] [MC] AsmLexer assert buffer is null-terminated at CurBuf.end() (PR #154972)
Szymon Piotr Milczek via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 31 03:00:31 PDT 2025
https://github.com/smilczek updated https://github.com/llvm/llvm-project/pull/154972
>From 61bf56a35365dc0112c484328afc4767e34c2a72 Mon Sep 17 00:00:00 2001
From: "Milczek, Szymon" <szymon.milczek at intel.com>
Date: Fri, 22 Aug 2025 17:44:51 +0200
Subject: [PATCH] [MCParser] AsmLexer assert buffer is null-terminated at
CurBuf.end()
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.
---
llvm/include/llvm/MC/MCParser/AsmLexer.h | 7 +++++++
llvm/lib/MC/MCParser/AsmLexer.cpp | 5 +++++
2 files changed, 12 insertions(+)
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<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
@@ -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 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