[llvm] r296494 - [PDB] Add BinaryStreamError.

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 28 09:49:34 PST 2017


Author: zturner
Date: Tue Feb 28 11:49:34 2017
New Revision: 296494

URL: http://llvm.org/viewvc/llvm-project?rev=296494&view=rev
Log:
[PDB] Add BinaryStreamError.

This migrates the stream code away from MSFError to using its
own custom Error class.

Added:
    llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamError.h
    llvm/trunk/lib/DebugInfo/MSF/BinaryStreamError.cpp
Modified:
    llvm/trunk/include/llvm/DebugInfo/MSF/BinaryByteStream.h
    llvm/trunk/include/llvm/DebugInfo/MSF/BinaryItemStream.h
    llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStream.h
    llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamReader.h
    llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamRef.h
    llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamWriter.h
    llvm/trunk/lib/DebugInfo/MSF/BinaryStreamReader.cpp
    llvm/trunk/lib/DebugInfo/MSF/CMakeLists.txt
    llvm/trunk/lib/DebugInfo/MSF/MappedBlockStream.cpp
    llvm/trunk/lib/DebugInfo/PDB/Native/PDBFile.cpp
    llvm/trunk/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp

Modified: llvm/trunk/include/llvm/DebugInfo/MSF/BinaryByteStream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/MSF/BinaryByteStream.h?rev=296494&r1=296493&r2=296494&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/MSF/BinaryByteStream.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/MSF/BinaryByteStream.h Tue Feb 28 11:49:34 2017
@@ -14,7 +14,7 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/DebugInfo/MSF/BinaryStream.h"
-#include "llvm/DebugInfo/MSF/MSFError.h"
+#include "llvm/DebugInfo/MSF/BinaryStreamError.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FileOutputBuffer.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -41,21 +41,16 @@ public:
 
   Error readBytes(uint32_t Offset, uint32_t Size,
                   ArrayRef<uint8_t> &Buffer) override {
-    if (Offset > Data.size())
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
-    if (Data.size() < Size + Offset)
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
+    if (auto EC = checkOffset(Offset, Size))
+      return EC;
     Buffer = Data.slice(Offset, Size);
     return Error::success();
   }
 
   Error readLongestContiguousChunk(uint32_t Offset,
                                    ArrayRef<uint8_t> &Buffer) override {
-    if (Offset >= Data.size())
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
+    if (auto EC = checkOffset(Offset, 1))
+      return EC;
     Buffer = Data.slice(Offset);
     return Error::success();
   }
@@ -119,12 +114,8 @@ public:
     if (Buffer.empty())
       return Error::success();
 
-    if (Data.size() < Buffer.size())
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
-    if (Offset > Buffer.size() - Data.size())
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
+    if (auto EC = checkOffset(Offset, Buffer.size()))
+      return EC;
 
     uint8_t *DataPtr = const_cast<uint8_t *>(Data.data());
     ::memcpy(DataPtr + Offset, Buffer.data(), Buffer.size());
@@ -156,8 +147,8 @@ private:
 
     Error commit() override {
       if (FileBuffer->commit())
-        return make_error<msf::MSFError>(
-            msf::msf_error_code::insufficient_buffer);
+        return make_error<BinaryStreamError>(
+            stream_error_code::filesystem_error);
       return Error::success();
     }
 

Modified: llvm/trunk/include/llvm/DebugInfo/MSF/BinaryItemStream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/MSF/BinaryItemStream.h?rev=296494&r1=296493&r2=296494&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/MSF/BinaryItemStream.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/MSF/BinaryItemStream.h Tue Feb 28 11:49:34 2017
@@ -12,7 +12,7 @@
 
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/DebugInfo/MSF/BinaryStream.h"
-#include "llvm/DebugInfo/MSF/MSFError.h"
+#include "llvm/DebugInfo/MSF/BinaryStreamError.h"
 #include "llvm/Support/Error.h"
 #include <cstddef>
 #include <cstdint>
@@ -45,9 +45,10 @@ public:
     if (!ExpectedIndex)
       return ExpectedIndex.takeError();
     const auto &Item = Items[*ExpectedIndex];
+    if (auto EC = checkOffset(Offset, Size))
+      return EC;
     if (Size > Traits::length(Item))
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
+      return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
     Buffer = Traits::bytes(Item).take_front(Size);
     return Error::success();
   }
@@ -81,8 +82,7 @@ private:
       ++CurrentIndex;
     }
     if (CurrentOffset != Offset)
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
+      return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
     return CurrentIndex;
   }
 

Modified: llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStream.h?rev=296494&r1=296493&r2=296494&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStream.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStream.h Tue Feb 28 11:49:34 2017
@@ -11,6 +11,7 @@
 #define LLVM_DEBUGINFO_MSF_BINARYSTREAM_H
 
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/DebugInfo/MSF/BinaryStreamError.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/Error.h"
 #include <cstdint>
@@ -43,6 +44,15 @@ public:
 
   /// \brief Return the number of bytes of data in this stream.
   virtual uint32_t getLength() = 0;
+
+protected:
+  Error checkOffset(uint32_t Offset, uint32_t DataSize) {
+    if (Offset > getLength())
+      return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
+    if (getLength() < DataSize + Offset)
+      return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
+    return Error::success();
+  }
 };
 
 /// \brief A BinaryStream which can be read from as well as written to.  Note

Added: llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamError.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamError.h?rev=296494&view=auto
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamError.h (added)
+++ llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamError.h Tue Feb 28 11:49:34 2017
@@ -0,0 +1,47 @@
+//===- BinaryStreamError.h - Error extensions for Binary Streams *- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_DEBUGINFO_MSF_BINARYSTREAMERROR_H
+#define LLVM_DEBUGINFO_MSF_BINARYSTREAMERROR_H
+
+#include "llvm/Support/Error.h"
+
+#include <string>
+
+namespace llvm {
+enum class stream_error_code {
+  unspecified,
+  stream_too_short,
+  invalid_array_size,
+  invalid_offset,
+  filesystem_error
+};
+
+/// Base class for errors originating when parsing raw PDB files
+class BinaryStreamError : public ErrorInfo<BinaryStreamError> {
+public:
+  static char ID;
+  explicit BinaryStreamError(stream_error_code C);
+  explicit BinaryStreamError(StringRef Context);
+  BinaryStreamError(stream_error_code C, StringRef Context);
+
+  void log(raw_ostream &OS) const override;
+  std::error_code convertToErrorCode() const override;
+
+  StringRef getErrorMessage() const;
+
+  stream_error_code getErrorCode() const { return Code; }
+
+private:
+  std::string ErrMsg;
+  stream_error_code Code;
+};
+} // namespace llvm
+
+#endif // LLVM_DEBUGINFO_MSF_BINARYSTREAMERROR_H

Modified: llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamReader.h?rev=296494&r1=296493&r2=296494&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamReader.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamReader.h Tue Feb 28 11:49:34 2017
@@ -150,7 +150,8 @@ public:
     }
 
     if (NumElements > UINT32_MAX / sizeof(T))
-      return make_error<msf::MSFError>(msf::msf_error_code::insufficient_buffer);
+      return make_error<BinaryStreamError>(
+          stream_error_code::invalid_array_size);
 
     if (auto EC = readBytes(Bytes, NumElements * sizeof(T)))
       return EC;
@@ -193,16 +194,16 @@ public:
       Array = FixedStreamArray<T>();
       return Error::success();
     }
-    uint32_t Length = NumItems * sizeof(T);
-    if (Length / sizeof(T) != NumItems)
-      return errorCodeToError(
-          make_error_code(std::errc::illegal_byte_sequence));
-    if (Offset + Length > Stream.getLength())
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
-    BinaryStreamRef View = Stream.slice(Offset, Length);
+
+    if (NumItems > UINT32_MAX / sizeof(T))
+      return make_error<BinaryStreamError>(
+          stream_error_code::invalid_array_size);
+
+    BinaryStreamRef View;
+    if (auto EC = readStreamRef(View, NumItems * sizeof(T)))
+      return EC;
+
     Array = FixedStreamArray<T>(View);
-    Offset += Length;
     return Error::success();
   }
 

Modified: llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamRef.h?rev=296494&r1=296493&r2=296494&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamRef.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamRef.h Tue Feb 28 11:49:34 2017
@@ -12,7 +12,7 @@
 
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/DebugInfo/MSF/BinaryStream.h"
-#include "llvm/DebugInfo/MSF/MSFError.h"
+#include "llvm/DebugInfo/MSF/BinaryStreamError.h"
 #include "llvm/Support/Error.h"
 #include <algorithm>
 #include <cstdint>
@@ -65,6 +65,14 @@ public:
   }
 
 protected:
+  Error checkOffset(uint32_t Offset, uint32_t DataSize) const {
+    if (Offset > getLength())
+      return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
+    if (getLength() < DataSize + Offset)
+      return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
+    return Error::success();
+  }
+
   StreamType *Stream;
   uint32_t ViewOffset;
   uint32_t Length;
@@ -98,12 +106,9 @@ public:
   /// the data, and an appropriate error code otherwise.
   Error readBytes(uint32_t Offset, uint32_t Size,
                   ArrayRef<uint8_t> &Buffer) const {
-    if (ViewOffset + Offset < Offset)
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
-    if (Size + Offset > Length)
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
+    if (auto EC = checkOffset(Offset, Size))
+      return EC;
+
     return Stream->readBytes(ViewOffset + Offset, Size, Buffer);
   }
 
@@ -114,9 +119,8 @@ public:
   /// and an appropriate error code otherwise.
   Error readLongestContiguousChunk(uint32_t Offset,
                                    ArrayRef<uint8_t> &Buffer) const {
-    if (Offset >= Length)
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
+    if (auto EC = checkOffset(Offset, 1))
+      return EC;
 
     if (auto EC = Stream->readLongestContiguousChunk(Offset, Buffer))
       return EC;
@@ -152,9 +156,9 @@ public:
   /// stream at the specified location and the implementation could write the
   /// data, and an appropriate error code otherwise.
   Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) const {
-    if (Data.size() + Offset > Length)
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
+    if (auto EC = checkOffset(Offset, Data.size()))
+      return EC;
+
     return Stream->writeBytes(ViewOffset + Offset, Data);
   }
 

Modified: llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamWriter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamWriter.h?rev=296494&r1=296493&r2=296494&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamWriter.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/MSF/BinaryStreamWriter.h Tue Feb 28 11:49:34 2017
@@ -14,6 +14,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/DebugInfo/MSF/BinaryStreamArray.h"
+#include "llvm/DebugInfo/MSF/BinaryStreamError.h"
 #include "llvm/DebugInfo/MSF/BinaryStreamRef.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/Error.h"
@@ -123,10 +124,9 @@ public:
   template <typename T> Error writeArray(ArrayRef<T> Array) {
     if (Array.empty())
       return Error::success();
-
     if (Array.size() > UINT32_MAX / sizeof(T))
-      return make_error<msf::MSFError>(
-          msf::msf_error_code::insufficient_buffer);
+      return make_error<BinaryStreamError>(
+          stream_error_code::invalid_array_size);
 
     return writeBytes(
         ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Array.data()),

Added: llvm/trunk/lib/DebugInfo/MSF/BinaryStreamError.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/MSF/BinaryStreamError.cpp?rev=296494&view=auto
==============================================================================
--- llvm/trunk/lib/DebugInfo/MSF/BinaryStreamError.cpp (added)
+++ llvm/trunk/lib/DebugInfo/MSF/BinaryStreamError.cpp Tue Feb 28 11:49:34 2017
@@ -0,0 +1,58 @@
+//===- BinaryStreamError.cpp - Error extensions for streams -----*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/DebugInfo/MSF/BinaryStreamError.h"
+#include "llvm/Support/ErrorHandling.h"
+
+using namespace llvm;
+
+char BinaryStreamError::ID = 0;
+
+BinaryStreamError::BinaryStreamError(stream_error_code C)
+    : BinaryStreamError(C, "") {}
+
+BinaryStreamError::BinaryStreamError(StringRef Context)
+    : BinaryStreamError(stream_error_code::unspecified, Context) {}
+
+BinaryStreamError::BinaryStreamError(stream_error_code C, StringRef Context)
+    : Code(C) {
+  ErrMsg = "Stream Error: ";
+  switch (C) {
+  case stream_error_code::unspecified:
+    ErrMsg += "An unspecified error has occurred.";
+    break;
+  case stream_error_code::stream_too_short:
+    ErrMsg += "The stream is too short to perform the requested operation.";
+    break;
+  case stream_error_code::invalid_array_size:
+    ErrMsg += "The buffer size is not a multiple of the array element size.";
+    break;
+  case stream_error_code::invalid_offset:
+    ErrMsg += "The specified offset is invalid for the current stream.";
+    break;
+  case stream_error_code::filesystem_error:
+    ErrMsg += "An I/O error occurred on the file system.";
+    break;
+  default:
+    llvm_unreachable("Unreachable!");
+  }
+
+  if (!Context.empty()) {
+    ErrMsg += "  ";
+    ErrMsg += Context;
+  }
+}
+
+void BinaryStreamError::log(raw_ostream &OS) const { OS << ErrMsg << "\n"; }
+
+StringRef BinaryStreamError::getErrorMessage() const { return ErrMsg; }
+
+std::error_code BinaryStreamError::convertToErrorCode() const {
+  return inconvertibleErrorCode();
+}

Modified: llvm/trunk/lib/DebugInfo/MSF/BinaryStreamReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/MSF/BinaryStreamReader.cpp?rev=296494&r1=296493&r2=296494&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/MSF/BinaryStreamReader.cpp (original)
+++ llvm/trunk/lib/DebugInfo/MSF/BinaryStreamReader.cpp Tue Feb 28 11:49:34 2017
@@ -9,11 +9,10 @@
 
 #include "llvm/DebugInfo/MSF/BinaryStreamReader.h"
 
+#include "llvm/DebugInfo/MSF/BinaryStreamError.h"
 #include "llvm/DebugInfo/MSF/BinaryStreamRef.h"
-#include "llvm/DebugInfo/MSF/MSFError.h"
 
 using namespace llvm;
-using namespace llvm::msf;
 
 BinaryStreamReader::BinaryStreamReader(BinaryStreamRef S)
     : Stream(S), Offset(0) {}
@@ -74,7 +73,7 @@ Error BinaryStreamReader::readStreamRef(
 
 Error BinaryStreamReader::readStreamRef(BinaryStreamRef &Ref, uint32_t Length) {
   if (bytesRemaining() < Length)
-    return make_error<MSFError>(msf_error_code::insufficient_buffer);
+    return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
   Ref = Stream.slice(Offset, Length);
   Offset += Length;
   return Error::success();
@@ -82,7 +81,7 @@ Error BinaryStreamReader::readStreamRef(
 
 Error BinaryStreamReader::skip(uint32_t Amount) {
   if (Amount > bytesRemaining())
-    return make_error<MSFError>(msf_error_code::insufficient_buffer);
+    return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
   Offset += Amount;
   return Error::success();
 }

Modified: llvm/trunk/lib/DebugInfo/MSF/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/MSF/CMakeLists.txt?rev=296494&r1=296493&r2=296494&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/MSF/CMakeLists.txt (original)
+++ llvm/trunk/lib/DebugInfo/MSF/CMakeLists.txt Tue Feb 28 11:49:34 2017
@@ -1,4 +1,5 @@
 add_llvm_library(LLVMDebugInfoMSF
+  BinaryStreamError.cpp
   BinaryStreamReader.cpp
   BinaryStreamWriter.cpp
   MappedBlockStream.cpp

Modified: llvm/trunk/lib/DebugInfo/MSF/MappedBlockStream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/MSF/MappedBlockStream.cpp?rev=296494&r1=296493&r2=296494&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/MSF/MappedBlockStream.cpp (original)
+++ llvm/trunk/lib/DebugInfo/MSF/MappedBlockStream.cpp Tue Feb 28 11:49:34 2017
@@ -9,9 +9,9 @@
 
 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
 
+#include "llvm/DebugInfo/MSF/BinaryStreamError.h"
 #include "llvm/DebugInfo/MSF/IMSFFile.h"
 #include "llvm/DebugInfo/MSF/MSFCommon.h"
-#include "llvm/DebugInfo/MSF/MSFError.h"
 #include "llvm/DebugInfo/MSF/MSFStreamLayout.h"
 
 using namespace llvm;
@@ -89,10 +89,8 @@ MappedBlockStream::createFpmStream(const
 Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
                                    ArrayRef<uint8_t> &Buffer) {
   // Make sure we aren't trying to read beyond the end of the stream.
-  if (Size > StreamLayout.Length)
-    return make_error<MSFError>(msf_error_code::insufficient_buffer);
-  if (Offset > StreamLayout.Length - Size)
-    return make_error<MSFError>(msf_error_code::insufficient_buffer);
+  if (auto EC = checkOffset(Offset, Size))
+    return EC;
 
   if (tryReadContiguously(Offset, Size, Buffer))
     return Error::success();
@@ -169,8 +167,9 @@ Error MappedBlockStream::readBytes(uint3
 Error MappedBlockStream::readLongestContiguousChunk(uint32_t Offset,
                                                     ArrayRef<uint8_t> &Buffer) {
   // Make sure we aren't trying to read beyond the end of the stream.
-  if (Offset >= StreamLayout.Length)
-    return make_error<MSFError>(msf_error_code::insufficient_buffer);
+  if (auto EC = checkOffset(Offset, 1))
+    return EC;
+
   uint32_t First = Offset / BlockSize;
   uint32_t Last = First;
 
@@ -244,10 +243,8 @@ Error MappedBlockStream::readBytes(uint3
   uint32_t OffsetInBlock = Offset % BlockSize;
 
   // Make sure we aren't trying to read beyond the end of the stream.
-  if (Buffer.size() > StreamLayout.Length)
-    return make_error<MSFError>(msf_error_code::insufficient_buffer);
-  if (Offset > StreamLayout.Length - Buffer.size())
-    return make_error<MSFError>(msf_error_code::insufficient_buffer);
+  if (auto EC = checkOffset(Offset, Buffer.size()))
+    return EC;
 
   uint32_t BytesLeft = Buffer.size();
   uint32_t BytesWritten = 0;
@@ -374,11 +371,8 @@ uint32_t WritableMappedBlockStream::getL
 Error WritableMappedBlockStream::writeBytes(uint32_t Offset,
                                             ArrayRef<uint8_t> Buffer) {
   // Make sure we aren't trying to write beyond the end of the stream.
-  if (Buffer.size() > getStreamLength())
-    return make_error<MSFError>(msf_error_code::insufficient_buffer);
-
-  if (Offset > getStreamLayout().Length - Buffer.size())
-    return make_error<MSFError>(msf_error_code::insufficient_buffer);
+  if (auto EC = checkOffset(Offset, Buffer.size()))
+    return EC;
 
   uint32_t BlockNum = Offset / getBlockSize();
   uint32_t OffsetInBlock = Offset % getBlockSize();

Modified: llvm/trunk/lib/DebugInfo/PDB/Native/PDBFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/PDB/Native/PDBFile.cpp?rev=296494&r1=296493&r2=296494&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/PDB/Native/PDBFile.cpp (original)
+++ llvm/trunk/lib/DebugInfo/PDB/Native/PDBFile.cpp Tue Feb 28 11:49:34 2017
@@ -396,11 +396,10 @@ bool PDBFile::hasStringTable() {
   return IS->getNamedStreamIndex("/names") < getNumStreams();
 }
 
-/// Wrapper around MappedBlockStream::createIndexedStream()
-/// that checks if a stream with that index actually exists.
-/// If it does not, the return value will have an MSFError with
-/// code msf_error_code::no_stream. Else, the return value will
-/// contain the stream returned by createIndexedStream().
+/// Wrapper around MappedBlockStream::createIndexedStream() that checks if a
+/// stream with that index actually exists.  If it does not, the return value
+/// will have an MSFError with code msf_error_code::no_stream.  Else, the return
+/// value will contain the stream returned by createIndexedStream().
 Expected<std::unique_ptr<MappedBlockStream>>
 PDBFile::safelyCreateIndexedStream(const MSFLayout &Layout,
                                    BinaryStreamRef MsfData,

Modified: llvm/trunk/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp?rev=296494&r1=296493&r2=296494&view=diff
==============================================================================
--- llvm/trunk/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp (original)
+++ llvm/trunk/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp Tue Feb 28 11:49:34 2017
@@ -42,16 +42,16 @@ public:
 
   Error readBytes(uint32_t Offset, uint32_t Size,
                   ArrayRef<uint8_t> &Buffer) override {
-    if (Offset + Size > Data.size())
-      return make_error<MSFError>(msf_error_code::insufficient_buffer);
+    if (auto EC = checkOffset(Offset, Size))
+      return EC;
     Buffer = Data.slice(Offset, Size);
     return Error::success();
   }
 
   Error readLongestContiguousChunk(uint32_t Offset,
                                    ArrayRef<uint8_t> &Buffer) override {
-    if (Offset >= Data.size())
-      return make_error<MSFError>(msf_error_code::insufficient_buffer);
+    if (auto EC = checkOffset(Offset, 1))
+      return EC;
     Buffer = Data.drop_front(Offset);
     return Error::success();
   }
@@ -59,8 +59,8 @@ public:
   uint32_t getLength() override { return Data.size(); }
 
   Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> SrcData) override {
-    if (Offset + SrcData.size() > Data.size())
-      return make_error<MSFError>(msf_error_code::insufficient_buffer);
+    if (auto EC = checkOffset(Offset, SrcData.size()))
+      return EC;
     ::memcpy(&Data[Offset], SrcData.data(), SrcData.size());
     return Error::success();
   }




More information about the llvm-commits mailing list