1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 03:33:20 +01:00

[CodeView] Make sure StreamRef::readBytes doesn't read too much

llvm-svn: 271418
This commit is contained in:
David Majnemer 2016-06-01 18:13:06 +00:00
parent 73419abbaa
commit 818581cd0c
2 changed files with 21 additions and 6 deletions

View File

@ -10,6 +10,7 @@
#ifndef LLVM_DEBUGINFO_CODEVIEW_STREAMREF_H
#define LLVM_DEBUGINFO_CODEVIEW_STREAMREF_H
#include "llvm/DebugInfo/CodeView/CodeViewError.h"
#include "llvm/DebugInfo/CodeView/StreamInterface.h"
namespace llvm {
@ -28,6 +29,8 @@ public:
Error readBytes(uint32_t Offset, uint32_t Size,
ArrayRef<uint8_t> &Buffer) const override {
if (Size > Length)
return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
return Stream->readBytes(ViewOffset + Offset, Size, Buffer);
}
@ -74,4 +77,4 @@ private:
}
}
#endif // LLVM_DEBUGINFO_CODEVIEW_STREAMREF_H
#endif // LLVM_DEBUGINFO_CODEVIEW_STREAMREF_H

View File

@ -71,14 +71,14 @@ private:
// Tests that a read which is entirely contained within a single block works
// and does not allocate.
TEST(MappedBlockStreamTest, ZeroCopyReadNoBreak) {
TEST(MappedBlockStreamTest, ReadBeyondEndOfStreamRef) {
DiscontiguousFile F;
MappedBlockStream S(0, F);
StreamReader R(S);
StringRef Str;
EXPECT_NO_ERROR(R.readFixedString(Str, 1));
EXPECT_EQ(Str, StringRef("A"));
EXPECT_EQ(0U, S.getNumBytesCopied());
StreamRef SR;
EXPECT_NO_ERROR(R.readStreamRef(SR, 0U));
ArrayRef<uint8_t> Buffer;
EXPECT_ERROR(SR.readBytes(0U, 1U, Buffer));
}
// Tests that a read which outputs into a full destination buffer works and
@ -162,4 +162,16 @@ TEST(MappedBlockStreamTest, InvalidReadSizeNonContiguousBreak) {
EXPECT_EQ(0U, S.getNumBytesCopied());
}
// Tests that a read which is entirely contained within a single block but
// beyond the end of a StreamRef fails.
TEST(MappedBlockStreamTest, ZeroCopyReadNoBreak) {
DiscontiguousFile F;
MappedBlockStream S(0, F);
StreamReader R(S);
StringRef Str;
EXPECT_NO_ERROR(R.readFixedString(Str, 1));
EXPECT_EQ(Str, StringRef("A"));
EXPECT_EQ(0U, S.getNumBytesCopied());
}
} // end anonymous namespace