Fix integer overflow bug in raw_ostream::write. This showed up as a
authorNick Lewycky <nicholas@mxc.ca>
Sun, 28 Aug 2011 03:30:02 +0000 (03:30 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Sun, 28 Aug 2011 03:30:02 +0000 (03:30 +0000)
non-deterministic crash in the test suite. Fixes PR10055!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@138717 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/raw_ostream.cpp

index 5a71fa3d8cea8f47fa82ce0cc795361d0f33c439..c9cf249500d62064ca816bac2467205f0a93928e 100644 (file)
@@ -84,7 +84,7 @@ void raw_ostream::SetBuffered() {
 }
 
 void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
-                                    BufferKind Mode) {
+                                   BufferKind Mode) {
   assert(((Mode == Unbuffered && BufferStart == 0 && Size == 0) ||
           (Mode != Unbuffered && BufferStart && Size)) &&
          "stream must be unbuffered or have at least one byte");
@@ -284,7 +284,7 @@ raw_ostream &raw_ostream::write(unsigned char C) {
 
 raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
   // Group exceptional cases into a single branch.
-  if (BUILTIN_EXPECT(OutBufCur+Size > OutBufEnd, false)) {
+  if (BUILTIN_EXPECT(size_t(OutBufEnd - OutBufCur) < Size, false)) {
     if (BUILTIN_EXPECT(!OutBufStart, false)) {
       if (BufferMode == Unbuffered) {
         write_impl(Ptr, Size);