diff options
author | Hal Canary <halcanary@google.com> | 2017-12-20 13:09:26 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-12-20 19:46:28 +0000 |
commit | 5ade1f6fd91371a2b985986ff099cc37836a443d (patch) | |
tree | 613915248ea3bdf06a68434fca25fe844f0c6041 /src/core/SkRegion.cpp | |
parent | 915817b940b05882d85c220d2b2dc18ef184a6b4 (diff) |
SkRegion: validate_run() checks for overflow
Change-Id: I9ffe9bcc5a59252e45b05b73b96912caf7da5c8b
Reviewed-on: https://skia-review.googlesource.com/87766
Reviewed-by: Herb Derby <herb@google.com>
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>
Diffstat (limited to 'src/core/SkRegion.cpp')
-rw-r--r-- | src/core/SkRegion.cpp | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/src/core/SkRegion.cpp b/src/core/SkRegion.cpp index dd70d1f507..30978854c5 100644 --- a/src/core/SkRegion.cpp +++ b/src/core/SkRegion.cpp @@ -8,6 +8,7 @@ #include "SkAtomics.h" #include "SkRegionPriv.h" +#include "SkSafeMath.h" #include "SkTemplates.h" #include "SkUtils.h" @@ -1129,6 +1130,21 @@ size_t SkRegion::writeToMemory(void* storage) const { return buffer.pos(); } +static bool validate_run_count(int ySpanCount, int intervalCount, int runCount) { + // return 2 + 3 * ySpanCount + 2 * intervalCount; + if (ySpanCount < 1 || intervalCount < 2) { + return false; + } + SkSafeMath safeMath; + int sum = 2; + sum = safeMath.addInt(sum, ySpanCount); + sum = safeMath.addInt(sum, ySpanCount); + sum = safeMath.addInt(sum, ySpanCount); + sum = safeMath.addInt(sum, intervalCount); + sum = safeMath.addInt(sum, intervalCount); + return safeMath && sum == runCount; +} + // Validate that a memory sequence is a valid region. // Try to check all possible errors. // never read beyond &runs[runCount-1]. @@ -1139,7 +1155,7 @@ static bool validate_run(const int32_t* runs, int32_t intervalCount) { // Region Layout: // Top ( Bottom Span_Interval_Count ( Left Right )* Sentinel )+ Sentinel - if (ySpanCount < 1 || intervalCount < 2 || runCount != 2 + 3 * ySpanCount + 2 * intervalCount) { + if (!validate_run_count(SkToInt(ySpanCount), SkToInt(intervalCount), runCount)) { return false; } SkASSERT(runCount >= 7); // 7==SkRegion::kRectRegionRuns |