From 4e2435bb71b393c20a9ef637894389b74c5a514c Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Mon, 4 May 2015 22:30:29 +0000 Subject: [PATCH] Fix buffer overflow in Lexer 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236466 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Lex/Lexer.cpp | 2 +- test/Lexer/eof-include.c | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 test/Lexer/eof-include.c diff --git a/lib/Lex/Lexer.cpp b/lib/Lex/Lexer.cpp index a3b520b263..3f89ea649c 100644 --- a/lib/Lex/Lexer.cpp +++ b/lib/Lex/Lexer.cpp @@ -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/test/Lexer/eof-include.c b/test/Lexer/eof-include.c new file mode 100644 index 0000000000..6e53788718 --- /dev/null +++ b/test/Lexer/eof-include.c @@ -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 }} +#include <\ \ No newline at end of file -- 2.50.1