Skip to content

Commit 077d8e8

Browse files
committed
[swiftBasic] Simplify bound checks for ExponentialGrowthAppendingBinaryByteStream
1 parent 49f2e14 commit 077d8e8

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

lib/Basic/ExponentialGrowthAppendingBinaryByteStream.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@ using namespace swift;
1717

1818
Error ExponentialGrowthAppendingBinaryByteStream::readBytes(
1919
uint32_t Offset, uint32_t Size, ArrayRef<uint8_t> &Buffer) {
20-
if (Offset > getLength())
21-
return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
22-
23-
if (Offset + Size > getLength())
24-
return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
25-
20+
if (auto Error = checkOffsetForRead(Offset, Size)) {
21+
return Error;
22+
}
23+
2624
Buffer = ArrayRef<uint8_t>(Data.data() + Offset, Size);
2725
return Error::success();
2826
}
2927

3028
Error ExponentialGrowthAppendingBinaryByteStream::readLongestContiguousChunk(
3129
uint32_t Offset, ArrayRef<uint8_t> &Buffer) {
32-
if (Offset > getLength())
33-
return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
30+
if (auto Error = checkOffsetForRead(Offset, 0)) {
31+
return Error;
32+
}
3433

3534
Buffer = ArrayRef<uint8_t>(Data.data() + Offset, Data.size() - Offset);
3635
return Error::success();
@@ -45,9 +44,10 @@ Error ExponentialGrowthAppendingBinaryByteStream::writeBytes(
4544
if (Buffer.empty())
4645
return Error::success();
4746

48-
if (Offset > getLength())
49-
return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
50-
47+
if (auto Error = checkOffsetForWrite(Offset, Buffer.size())) {
48+
return Error;
49+
}
50+
5151
// Resize the internal buffer if needed.
5252
uint32_t RequiredSize = Offset + Buffer.size();
5353
if (RequiredSize > Data.size()) {

0 commit comments

Comments
 (0)