[llvm] r176911 - Don't crash if write_impl() leaves less buffer space available than expected.

Matt Beaumont-Gay matthewbg at google.com
Tue Mar 12 16:55:24 PDT 2013


Author: matthewbg
Date: Tue Mar 12 18:55:24 2013
New Revision: 176911

URL: http://llvm.org/viewvc/llvm-project?rev=176911&view=rev
Log:
Don't crash if write_impl() leaves less buffer space available than expected.

This was tickled by a Clang diagnostic; Clang test case to follow.

Modified:
    llvm/trunk/lib/Support/raw_ostream.cpp

Modified: llvm/trunk/lib/Support/raw_ostream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=176911&r1=176910&r2=176911&view=diff
==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Tue Mar 12 18:55:24 2013
@@ -306,7 +306,12 @@ raw_ostream &raw_ostream::write(const ch
     if (LLVM_UNLIKELY(OutBufCur == OutBufStart)) {
       size_t BytesToWrite = Size - (Size % NumBytes);
       write_impl(Ptr, BytesToWrite);
-      copy_to_buffer(Ptr + BytesToWrite, Size - BytesToWrite);
+      size_t BytesRemaining = Size - BytesToWrite;
+      if (BytesRemaining > size_t(OutBufEnd - OutBufCur)) {
+        // Too much left over to copy into our buffer.
+        return write(Ptr + BytesToWrite, BytesRemaining);
+      }
+      copy_to_buffer(Ptr + BytesToWrite, BytesRemaining);
       return *this;
     }
 





More information about the llvm-commits mailing list