Skip to content

Commit cbc22f2

Browse files
committed
chore: update BlobDescriptorStream logic to prevent over-read if more bytes are returned than requested
1 parent f7c37f5 commit cbc22f2

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/BlobDescriptorStream.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,21 @@ public void onResponse(BidiReadObjectResponse response) {
281281
long begin = readRange.getReadOffset();
282282
long position = read.getReadCursor().position();
283283
if (begin == position) {
284-
ChildRef childRef =
285-
handle.borrow(r -> r.getObjectDataRanges(idx).getChecksummedData().getContent());
284+
long remaining = read.getReadCursor().remaining();
285+
int contentSize = content.size();
286+
ChildRef childRef;
287+
if (remaining >= contentSize) {
288+
childRef =
289+
handle.borrow(r -> r.getObjectDataRanges(idx).getChecksummedData().getContent());
290+
} else {
291+
childRef =
292+
handle.borrow(
293+
r ->
294+
r.getObjectDataRanges(idx)
295+
.getChecksummedData()
296+
.getContent()
297+
.substring(0, Math.toIntExact(remaining)));
298+
}
286299
read.accept(childRef);
287300
} else if (begin < position) {
288301
int skip = Math.toIntExact(position - begin);

google-cloud-storage/src/test/java/com/google/cloud/storage/ITBlobDescriptorFakeTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,31 @@ public void retrySettingsApplicable_objectRangeData_offset_notAligned_gt() throw
704704
}
705705
}
706706

707+
@Test
708+
public void moreBytesReturnedThanRequested_onlyForwardsRequestedBytes() throws Exception {
709+
710+
ChecksummedTestContent expected = ChecksummedTestContent.of(ALL_OBJECT_BYTES, 10, 20);
711+
ChecksummedTestContent content2 = ChecksummedTestContent.of(ALL_OBJECT_BYTES, 10, 21);
712+
BidiReadObjectRequest req2 = read(1, 10, 20);
713+
BidiReadObjectResponse res2 =
714+
BidiReadObjectResponse.newBuilder()
715+
.addObjectDataRanges(
716+
ObjectRangeData.newBuilder()
717+
.setChecksummedData(content2.asChecksummedData())
718+
.setReadRange(getReadRange(1, 10, content2))
719+
.setRangeEnd(true)
720+
.build())
721+
.build();
722+
723+
ImmutableMap<BidiReadObjectRequest, BidiReadObjectResponse> db =
724+
ImmutableMap.<BidiReadObjectRequest, BidiReadObjectResponse>builder()
725+
.put(REQ_OPEN, RES_OPEN)
726+
.put(req2, res2)
727+
.buildOrThrow();
728+
729+
runTestAgainstFakeServer(FakeStorage.from(db), RangeSpec.of(10, 20), expected);
730+
}
731+
707732
private static void runTestAgainstFakeServer(
708733
FakeStorage fakeStorage, RangeSpec range, ChecksummedTestContent expected) throws Exception {
709734

0 commit comments

Comments
 (0)