[PATCH] Fix buffer overflow in Lexer

Kostya Serebryany kcc at google.com
Mon May 4 15:09:58 PDT 2015


Hi rnk,

Fix PR22407, where the Lexer overflows the buffer when parsing
 #include<\
(end of file after slash)

http://reviews.llvm.org/D9489

Files:
  lib/Lex/Lexer.cpp
  test/Lexer/eof-include.c

Index: lib/Lex/Lexer.cpp
===================================================================
--- lib/Lex/Lexer.cpp
+++ lib/Lex/Lexer.cpp
@@ -1854,7 +1854,7 @@
   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.
Index: test/Lexer/eof-include.c
===================================================================
--- /dev/null
+++ 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 at +1{{expected "FILENAME" or <FILENAME>}}
+#include <\
\ No newline at end of file

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9489.24920.patch
Type: text/x-patch
Size: 902 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150504/4bebb34d/attachment.bin>


More information about the cfe-commits mailing list