don't divide by zero when colorCount=1

This is my super lazy fix for the attached, completely unimportant
false-postive bug.  I haven't even paused to consider whether
colorCount=1 makes sense here.

An alternative is to use sk_ieee_double_divide() here to say "divide by
zero is fine, man".  But I kind of like this: wScale will hoist out of
the loop just fine, and it's nice to have the pos==nullptr checks near
each other.

PS3: yep, we can.

Bug: oss-fuzz:24242
Change-Id: I380ff2610405488cdaf475c34617bc9b4427ade9
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/304019
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
This commit is contained in:
Mike Klein 2020-07-20 09:59:16 -05:00 committed by Skia Commit-Bot
parent af1103040e
commit a17799aa59

View File

@ -694,17 +694,18 @@ static SkColor4f average_gradient_color(const SkColor4f colors[], const SkScalar
// the integral between the two endpoints is 0.5 * (ci + cj) * (pj - pi), which provides that // the integral between the two endpoints is 0.5 * (ci + cj) * (pj - pi), which provides that
// intervals average color. The overall average color is thus the sum of each piece. The thing // intervals average color. The overall average color is thus the sum of each piece. The thing
// to keep in mind is that the provided gradient definition may implicitly use p=0 and p=1. // to keep in mind is that the provided gradient definition may implicitly use p=0 and p=1.
Sk4f blend(0.0); Sk4f blend(0.0f);
// Bake 1/(colorCount - 1) uniform stop difference into this scale factor
SkScalar wScale = pos ? 0.5 : 0.5 / (colorCount - 1);
for (int i = 0; i < colorCount - 1; ++i) { for (int i = 0; i < colorCount - 1; ++i) {
// Calculate the average color for the interval between pos(i) and pos(i+1) // Calculate the average color for the interval between pos(i) and pos(i+1)
Sk4f c0 = Sk4f::Load(&colors[i]); Sk4f c0 = Sk4f::Load(&colors[i]);
Sk4f c1 = Sk4f::Load(&colors[i + 1]); Sk4f c1 = Sk4f::Load(&colors[i + 1]);
// when pos == null, there are colorCount uniformly distributed stops, going from 0 to 1, // when pos == null, there are colorCount uniformly distributed stops, going from 0 to 1,
// so pos[i + 1] - pos[i] = 1/(colorCount-1) // so pos[i + 1] - pos[i] = 1/(colorCount-1)
SkScalar w = pos ? (pos[i + 1] - pos[i]) : SK_Scalar1; SkScalar w = pos ? (pos[i + 1] - pos[i])
blend += wScale * w * (c1 + c0); : (1.0f / (colorCount - 1));
blend += 0.5f * w * (c1 + c0);
} }
// Now account for any implicit intervals at the start or end of the stop definitions // Now account for any implicit intervals at the start or end of the stop definitions