]> granicus.if.org Git - llvm/commitdiff
[MC] Fix compiler crash in AsmParser::Lex
authorNirav Dave <niravd@google.com>
Fri, 9 Jun 2017 14:04:03 +0000 (14:04 +0000)
committerNirav Dave <niravd@google.com>
Fri, 9 Jun 2017 14:04:03 +0000 (14:04 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305077 91177308-0d34-0410-b5e6-96231b3b80d8

lib/MC/MCParser/AsmParser.cpp
test/MC/AsmParser/empty-comment.s [new file with mode: 0644]

index 2e6ca7be15fee27bff7f46c9d403471154161e92..dad47e49e2c2062e2aaa3c6b619dbafc78e9b0ff 100644 (file)
@@ -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(ParseStatementInfo &Info,
     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();
diff --git a/test/MC/AsmParser/empty-comment.s b/test/MC/AsmParser/empty-comment.s
new file mode 100644 (file)
index 0000000..57df820
--- /dev/null
@@ -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