SkM44::LookAt: handle zero-length vectors gracefully

Zero-length vectors cannot be normalized.

Bug: oss-fuzz:38738
Change-Id: I6c5f4114f70a8a9f03afc1cd70c871e6b6b520ad
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/536637
Auto-Submit: Florin Malita <fmalita@chromium.org>
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Florin Malita <fmalita@chromium.org>
Commit-Queue: Kevin Lubick <kjlubick@google.com>
This commit is contained in:
Florin Malita 2022-05-04 10:24:14 -04:00 committed by SkCQ
parent efb1290cdf
commit 722d1c0679
2 changed files with 11 additions and 1 deletions

View File

@ -321,7 +321,11 @@ SkM44 SkM44::RectToRect(const SkRect& src, const SkRect& dst) {
0.f, 0.f, 0.f, 1.f};
}
static SkV3 normalize(SkV3 v) { return v * (1.0f / v.length()); }
static SkV3 normalize(SkV3 v) {
const auto vlen = v.length();
return SkScalarNearlyZero(vlen) ? v : v * (1.0f / vlen);
}
static SkV4 v4(SkV3 v, SkScalar w) { return {v.x, v.y, v.z, w}; }

View File

@ -1042,3 +1042,9 @@ DEF_TEST(Matrix_mapRect_skbug12335, r) {
DEF_TEST(Matrix_Ctor, r) {
REPORTER_ASSERT(r, SkMatrix{} == SkMatrix::I());
}
DEF_TEST(Matrix_LookAt, r) {
// Degenerate inputs should not trigger *SAN errors.
const auto m = SkM44::LookAt({0,0,0}, {0,0,0}, {0,0,0});
REPORTER_ASSERT(r, m == SkM44());
}