Skip to content

Commit

Permalink
[ganesh] Check index count for overflow for good measure
Browse files Browse the repository at this point in the history
Bug: b/381696867
Bug: b/381679570
Change-Id: Ica573d9143bf204015f905efecc45983177dfe83
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/926176
Reviewed-by: Robert Phillips <robertphillips@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
  • Loading branch information
lhkbob authored and SkCQ committed Dec 5, 2024
1 parent e94e63e commit 07cd080
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/gpu/ganesh/ops/GrOvalOpFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "include/private/base/SkFloatingPoint.h"
#include "include/private/base/SkOnce.h"
#include "include/private/base/SkTArray.h"
#include "include/private/base/SkTo.h"
#include "include/private/gpu/ganesh/GrTypesPriv.h"
#include "src/base/SkArenaAlloc.h"
#include "src/core/SkMatrixPriv.h"
Expand Down Expand Up @@ -61,6 +62,7 @@

#include <algorithm>
#include <array>
#include <climits>
#include <cstdint>
#include <memory>
#include <utility>
Expand Down Expand Up @@ -2831,8 +2833,11 @@ class CircularRRectOp final : public GrMeshDrawOp {
CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
CircularRRectOp* that = t->cast<CircularRRectOp>();

// can only represent 65535 unique vertices with 16-bit indices
if (fVertCount + that->fVertCount > 65536) {
// Cannot combine if the net number of indices would overflow int32, or if the net number
// of vertices would overflow uint16 (since the index values are 16-bit that point into
// the vertex buffer).
if ((fIndexCount > INT32_MAX - that->fIndexCount) ||
(fVertCount > SkToInt(UINT16_MAX) - that->fVertCount)) {
return CombineResult::kCannotCombine;
}

Expand Down
13 changes: 12 additions & 1 deletion src/gpu/ganesh/ops/ShadowRRectOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

#include <algorithm>
#include <array>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <tuple>
Expand Down Expand Up @@ -648,7 +649,8 @@ class ShadowCircularRRectOp final : public GrMeshDrawOp {
}

fMesh = target->allocMesh();
fMesh->setIndexed(std::move(indexBuffer), fIndexCount, firstIndex, 0, fVertCount - 1,
fMesh->setIndexed(std::move(indexBuffer), fIndexCount, firstIndex, 0,
SkTo<uint16_t>(fVertCount - 1),
GrPrimitiveRestart::kNo, std::move(vertexBuffer), firstVertex);
}

Expand All @@ -669,6 +671,15 @@ class ShadowCircularRRectOp final : public GrMeshDrawOp {

CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
ShadowCircularRRectOp* that = t->cast<ShadowCircularRRectOp>();

// Cannot combine if the net number of indices would overflow int32, or if the net number
// of vertices would overflow uint16 (since the index values are 16-bit that point into
// the vertex buffer).
if ((fIndexCount > INT32_MAX - that->fIndexCount) ||
(fVertCount > SkToInt(UINT16_MAX) - that->fVertCount)) {
return CombineResult::kCannotCombine;
}

fGeoData.push_back_n(that->fGeoData.size(), that->fGeoData.begin());
fVertCount += that->fVertCount;
fIndexCount += that->fIndexCount;
Expand Down

0 comments on commit 07cd080

Please sign in to comment.