[PATCH] D12854: [SourceManager] Support buffers that are not null-terminated

Keno Fischer via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 14 11:23:04 PDT 2015


loladiro created this revision.
loladiro added a reviewer: bkramer.
loladiro added a subscriber: cfe-commits.
loladiro set the repository for this revision to rL LLVM.

I feed custom buffers to clang, and since the buffers store an explicit length, I did not think to null-terminate them.
However, this causes clang to run out-of-bounds in certain situations in `ComputeLineNumbers`. Since under certain
situations `ComputeLineNumbers` does look at the given bounds, I figured the proper solution was to always check the
buffer for EOF. Please let me know if I missed something and this was intended.

Repository:
  rL LLVM

http://reviews.llvm.org/D12854

Files:
  lib/Basic/SourceManager.cpp

Index: lib/Basic/SourceManager.cpp
===================================================================
--- lib/Basic/SourceManager.cpp
+++ lib/Basic/SourceManager.cpp
@@ -1227,7 +1227,7 @@
 
     // First fix up the alignment to 16 bytes.
     while (((uintptr_t)NextBuf & 0xF) != 0) {
-      if (*NextBuf == '\n' || *NextBuf == '\r' || *NextBuf == '\0')
+      if (*NextBuf == '\n' || *NextBuf == '\r' || *NextBuf == '\0' || NextBuf == End)
         goto FoundSpecialChar;
       ++NextBuf;
     }
@@ -1248,24 +1248,25 @@
     }
 #endif
 
-    while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
+    while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0' && NextBuf == End)
       ++NextBuf;
 
 #ifdef __SSE2__
 FoundSpecialChar:
 #endif
     Offs += NextBuf-Buf;
     Buf = NextBuf;
 
+    // If end of file, exit.
+    if (Buf == End) break;
+
     if (Buf[0] == '\n' || Buf[0] == '\r') {
       // If this is \n\r or \r\n, skip both characters.
       if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
         ++Offs, ++Buf;
       ++Offs, ++Buf;
       LineOffsets.push_back(Offs);
     } else {
-      // Otherwise, this is a null.  If end of file, exit.
-      if (Buf == End) break;
       // Otherwise, skip the null.
       ++Offs, ++Buf;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12854.34708.patch
Type: text/x-patch
Size: 1290 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150914/1f3f3de5/attachment.bin>


More information about the cfe-commits mailing list