Assert when creating nullptr spans with a nonzero length.

According to https://en.cppreference.com/w/cpp/container/span/span (2),
it  is undefined behavior if [first, first + count) is not a valid
range, so it is standards-conforming to assert on this span.

This caught one case in GlyphRun where we were creating an invalid
span (which was apparently harmless in practice).

Change-Id: Ia0661b3bd13c2c5773492960eea11b1a294072d5
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/550499
Auto-Submit: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Herb Derby <herb@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
This commit is contained in:
John Stiles 2022-06-16 10:22:58 -04:00 committed by SkCQ
parent 9be648ad64
commit 40e3b3c605
2 changed files with 3 additions and 1 deletions

View File

@ -26,6 +26,7 @@ class SkSpan {
public:
constexpr SkSpan() : fPtr{nullptr}, fSize{0} {}
constexpr SkSpan(T* ptr, size_t size) : fPtr{ptr}, fSize{size} {
SkASSERT(ptr || size == 0); // disallow nullptr + a nonzero size
SkASSERT(size < kMaxSize);
}
template <typename U, typename = typename std::enable_if<std::is_same<const U, T>::value>::type>

View File

@ -263,12 +263,13 @@ const SkGlyphRunList& SkGlyphRunBuilder::blobToGlyphRunList(
}
}
const uint32_t* clusters = it.clusters();
this->makeGlyphRun(
font,
glyphIDs,
positions,
SkSpan<const char>(it.text(), it.textSize()),
SkSpan<const uint32_t>(it.clusters(), runSize),
SkSpan<const uint32_t>(clusters, clusters ? runSize : 0),
scaledRotations);
}