From 5a54977ea8ba690192d98c1fd4571858cc1451ac Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Sun, 14 Dec 2014 04:53:11 +0000 Subject: [PATCH] Lex: Don't crash if both conflict markers are on the same line We would check if the terminator marker is on a newline. However, the logic would end up out-of-bounds if the terminator marker immediately follows the start marker. This fixes PR21820. llvm-svn: 224210 --- clang/lib/Lex/Lexer.cpp | 4 ++-- clang/test/Lexer/conflict-marker.c | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index c2e9716123c4..ca5252e1c9ce 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -2588,8 +2588,8 @@ static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd, size_t Pos = RestOfBuffer.find(Terminator); while (Pos != StringRef::npos) { // Must occur at start of line. - if (RestOfBuffer[Pos-1] != '\r' && - RestOfBuffer[Pos-1] != '\n') { + if (Pos == 0 || + (RestOfBuffer[Pos - 1] != '\r' && RestOfBuffer[Pos - 1] != '\n')) { RestOfBuffer = RestOfBuffer.substr(Pos+TermLen); Pos = RestOfBuffer.find(Terminator); continue; diff --git a/clang/test/Lexer/conflict-marker.c b/clang/test/Lexer/conflict-marker.c index e5bc7f33e45f..2611827d2ace 100644 --- a/clang/test/Lexer/conflict-marker.c +++ b/clang/test/Lexer/conflict-marker.c @@ -36,3 +36,5 @@ int foo() { y a = x; return x + a - z; } + +<<<<<<<>>>>>>> // expected-error {{expected identifier}}