r236466 - Fix buffer overflow in Lexer

Kostya Serebryany kcc at google.com
Mon May 4 15:30:29 PDT 2015


Author: kcc
Date: Mon May  4 17:30:29 2015
New Revision: 236466

URL: http://llvm.org/viewvc/llvm-project?rev=236466&view=rev
Log:
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

Added:
    cfe/trunk/test/Lexer/eof-include.c
Modified:
    cfe/trunk/lib/Lex/Lexer.cpp

Modified: cfe/trunk/lib/Lex/Lexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Lexer.cpp?rev=236466&r1=236465&r2=236466&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Lexer.cpp (original)
+++ cfe/trunk/lib/Lex/Lexer.cpp Mon May  4 17:30:29 2015
@@ -1854,7 +1854,7 @@ bool Lexer::LexAngledStringLiteral(Token
   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.

Added: cfe/trunk/test/Lexer/eof-include.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/eof-include.c?rev=236466&view=auto
==============================================================================
--- cfe/trunk/test/Lexer/eof-include.c (added)
+++ cfe/trunk/test/Lexer/eof-include.c Mon May  4 17:30:29 2015
@@ -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 at +1{{expected "FILENAME" or <FILENAME>}}
+#include <\
\ No newline at end of file





More information about the cfe-commits mailing list