[llvm] [MC][AsmLexer] 'LexToken()': fix potential buffer overflow. (PR #105312)

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 20 12:55:52 PDT 2024


https://github.com/PavelKopyl created https://github.com/llvm/llvm-project/pull/105312

When the 'CurPtr' points to the 'EOF', calling either 'isAtStartOfComment', or 'isAtStatementSeparator' leads to dereferencing of 'CurBuf.end()'.
Usually this issue is hidden, as the AsmParser receives a source code via MemoryBuffer object with the null-terminating symbol, but the null-terminator is not required for AsmParser logic.

>From d7c8683a2f6dedfe85a11ad449597cdf7ed69242 Mon Sep 17 00:00:00 2001
From: Pavel Kopyl <pavelkopyl at gmail.com>
Date: Tue, 20 Aug 2024 21:45:36 +0200
Subject: [PATCH] [MC][AsmLexer] 'LexToken()': fix potential buffer overflow.

When the 'CurPtr' points to the 'EOF', calling either
'isAtStartOfComment', or 'isAtStatementSeparator' leads to
dereferencing of 'CurBuf.end()'.
Usually this issue is hidden, because the AsmParser receives a source
code via MemoryBuffer object with the null-terminating symbol, but the
null-terminator is not required for AsmParser logic.
---
 llvm/lib/MC/MCParser/AsmLexer.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/MC/MCParser/AsmLexer.cpp b/llvm/lib/MC/MCParser/AsmLexer.cpp
index 778ca340e12489..517e99c4a2915d 100644
--- a/llvm/lib/MC/MCParser/AsmLexer.cpp
+++ b/llvm/lib/MC/MCParser/AsmLexer.cpp
@@ -752,10 +752,10 @@ AsmToken AsmLexer::LexToken() {
       return LexLineComment();
   }
 
-  if (isAtStartOfComment(TokStart))
+  if (CurChar != EOF && isAtStartOfComment(TokStart))
     return LexLineComment();
 
-  if (isAtStatementSeparator(TokStart)) {
+  if (CurChar != EOF && isAtStatementSeparator(TokStart)) {
     CurPtr += strlen(MAI.getSeparatorString()) - 1;
     IsAtStartOfLine = true;
     IsAtStartOfStatement = true;



More information about the llvm-commits mailing list