Make SkROBuffer::Iter::size() work when exhausted

According to the documentation, this method will return 0 when the Iter
is exhausted. Prior to this CL, it crashes instead.

Prevent a crash with a null fHead, and add a test to verify the
behavior.
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1574603002

Review URL: https://codereview.chromium.org/1574603002
This commit is contained in:
scroggo 2016-01-11 06:38:00 -08:00 committed by Commit bot
parent 0b558cb42c
commit b512aaa5c8
2 changed files with 18 additions and 0 deletions

View File

@ -150,6 +150,9 @@ const void* SkROBuffer::Iter::data() const {
}
size_t SkROBuffer::Iter::size() const {
if (!fBlock) {
return 0;
}
return SkTMin(fBlock->fUsed, fRemaining);
}

View File

@ -313,3 +313,18 @@ DEF_TEST(RWBuffer, reporter) {
delete streams[i];
}
}
// Tests that it is safe to call SkROBuffer::Iter::size() when exhausted.
DEF_TEST(RWBuffer_size, r) {
SkRWBuffer buffer;
buffer.append(gABC, 26);
SkAutoTUnref<SkROBuffer> roBuffer(buffer.newRBufferSnapshot());
SkROBuffer::Iter iter(roBuffer);
REPORTER_ASSERT(r, iter.data());
REPORTER_ASSERT(r, iter.size() == 26);
// There is only one block in this buffer.
REPORTER_ASSERT(r, !iter.next());
REPORTER_ASSERT(r, 0 == iter.size());
}