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,7 +1248,7 @@ } #endif - while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') + while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0' && NextBuf == End) ++NextBuf; #ifdef __SSE2__ @@ -1257,6 +1257,9 @@ 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]) @@ -1264,8 +1267,6 @@ ++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; }