Fix buffer overflow in Lexer
authorKostya Serebryany <kcc@google.com>
Mon, 4 May 2015 22:30:29 +0000 (22:30 +0000)
committerKostya Serebryany <kcc@google.com>
Mon, 4 May 2015 22:30:29 +0000 (22:30 +0000)
Summary:
Fix PR22407, where the Lexer overflows the buffer when parsing
 #include<\
(end of file after slash)

Test Plan:
Added a test that will trigger in asan build.
This case is also covered by the clang-fuzzer bot.

Reviewers: rnk

Reviewed By: rnk

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D9489

llvm-svn: 236466

clang/lib/Lex/Lexer.cpp
clang/test/Lexer/eof-include.c [new file with mode: 0644]

index a3b520b..3f89ea6 100644 (file)
@@ -1854,7 +1854,7 @@ bool Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
   char C = getAndAdvanceChar(CurPtr, Result);
   while (C != '>') {
     // Skip escaped characters.
-    if (C == '\\') {
+    if (C == '\\' && CurPtr < BufferEnd) {
       // Skip the escaped character.
       getAndAdvanceChar(CurPtr, Result);
     } else if (C == '\n' || C == '\r' ||             // Newline.
diff --git a/clang/test/Lexer/eof-include.c b/clang/test/Lexer/eof-include.c
new file mode 100644 (file)
index 0000000..6e53788
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -verify
+// vim: set binary noeol:
+
+// This file intentionally ends without a \n on the last line.  Make sure your
+// editor doesn't add one.
+
+// expected-error@+1{{expected "FILENAME" or <FILENAME>}}
+#include <\
\ No newline at end of file