[llvm] r305077 - [MC] Fix compiler crash in AsmParser::Lex

Nirav Dave via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 9 07:04:03 PDT 2017


Author: niravd
Date: Fri Jun  9 09:04:03 2017
New Revision: 305077

URL: http://llvm.org/viewvc/llvm-project?rev=305077&view=rev
Log:
[MC] Fix compiler crash in AsmParser::Lex

When an empty comment is present in an assembly file, the compiler will crash because it checks the first character for '\n' or '\r'.
The fix consists of also checking if the string is empty before accessing the *front* method of the StringRef.
A test is included for the x86 target, but this issue is reproducible with other targets as well.

Patch by Alexandru Guduleasa!

Reviewers: niravd, grosbach, llvm-commits

Reviewed By: niravd

Differential Revision: https://reviews.llvm.org/D33993

Added:
    llvm/trunk/test/MC/AsmParser/empty-comment.s
Modified:
    llvm/trunk/lib/MC/MCParser/AsmParser.cpp

Modified: llvm/trunk/lib/MC/MCParser/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmParser.cpp?rev=305077&r1=305076&r2=305077&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp Fri Jun  9 09:04:03 2017
@@ -703,7 +703,7 @@ const AsmToken &AsmParser::Lex() {
   // if it's a end of statement with a comment in it
   if (getTok().is(AsmToken::EndOfStatement)) {
     // if this is a line comment output it.
-    if (getTok().getString().front() != '\n' &&
+    if (!getTok().getString().empty() && getTok().getString().front() != '\n' &&
         getTok().getString().front() != '\r' && MAI.preserveAsmComments())
       Out.addExplicitComment(Twine(getTok().getString()));
   }
@@ -1523,7 +1523,7 @@ bool AsmParser::parseStatement(ParseStat
     Lex();
   if (Lexer.is(AsmToken::EndOfStatement)) {
     // if this is a line comment we can drop it safely
-    if (getTok().getString().front() == '\r' ||
+    if (getTok().getString().empty() || getTok().getString().front() == '\r' ||
         getTok().getString().front() == '\n')
       Out.AddBlankLine();
     Lex();

Added: llvm/trunk/test/MC/AsmParser/empty-comment.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/empty-comment.s?rev=305077&view=auto
==============================================================================
--- llvm/trunk/test/MC/AsmParser/empty-comment.s (added)
+++ llvm/trunk/test/MC/AsmParser/empty-comment.s Fri Jun  9 09:04:03 2017
@@ -0,0 +1,4 @@
+	#RUN: llvm-mc -preserve-comments -n -triple i386-linux-gnu < %s > %t
+	.text
+foo:
+	nop #
\ No newline at end of file




More information about the llvm-commits mailing list