diff --git a/include/llvm/DebugInfo/CodeView/StreamRef.h b/include/llvm/DebugInfo/CodeView/StreamRef.h index 2242b6783e3..01b19138398 100644 --- a/include/llvm/DebugInfo/CodeView/StreamRef.h +++ b/include/llvm/DebugInfo/CodeView/StreamRef.h @@ -29,7 +29,7 @@ public: Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef &Buffer) const override { - if (Size > Length) + if (Size + Offset > Length) return make_error(cv_error_code::insufficient_buffer); return Stream->readBytes(ViewOffset + Offset, Size, Buffer); } diff --git a/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp b/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp index f40ac356a0d..75e954df411 100644 --- a/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp +++ b/unittests/DebugInfo/PDB/MappedBlockStreamTest.cpp @@ -79,6 +79,8 @@ TEST(MappedBlockStreamTest, ReadBeyondEndOfStreamRef) { EXPECT_NO_ERROR(R.readStreamRef(SR, 0U)); ArrayRef Buffer; EXPECT_ERROR(SR.readBytes(0U, 1U, Buffer)); + EXPECT_NO_ERROR(R.readStreamRef(SR, 1U)); + EXPECT_ERROR(SR.readBytes(1U, 1U, Buffer)); } // Tests that a read which outputs into a full destination buffer works and