Skia: Validate allocation size in GrBufferAllocPool using SkSafeMath.

Upstream port of commit 7469a9341afab19271b8ef07af5c16a0f2c4ccc1
This commit is contained in:
wolfbeast 2019-02-17 22:11:40 +01:00 committed by Roy Tam
commit 32c0424845

View file

@ -152,13 +152,18 @@ void* GrBufferAllocPool::makeSpace(size_t size,
BufferBlock& back = fBlocks.back();
size_t usedBytes = back.fBuffer->gpuMemorySize() - back.fBytesFree;
size_t pad = GrSizeAlignUpPad(usedBytes, alignment);
if ((size + pad) <= back.fBytesFree) {
SkSafeMath safeMath;
size_t alignedSize = safeMath.add(pad, size);
if (!safeMath.ok()) {
return nullptr;
}
if (alignedSize <= back.fBytesFree) {
memset((void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes), 0, pad);
usedBytes += pad;
*offset = usedBytes;
*buffer = back.fBuffer;
back.fBytesFree -= size + pad;
fBytesInUse += size + pad;
back.fBytesFree -= alignedSize;
fBytesInUse += alignedSize;
VALIDATE();
return (void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes);
}