Assert against infinite values of control points in CatmullRomSpline (#131820)

When providing infinite values for the control points of CatmullRomSpline, a StackOverflowError occurs. This asserts against that and provides a helpful error message. 

Fixes https://github.com/flutter/flutter/issues/131246
This commit is contained in:
Kate Lovett 2023-08-02 19:18:13 -05:00 committed by GitHub
parent 0ad45f2421
commit b3a4decda6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View file

@ -705,6 +705,27 @@ class CatmullRomSpline extends Curve2D {
Offset? startHandle,
Offset? endHandle,
}) {
assert(
startHandle == null || startHandle.isFinite,
'The provided startHandle of CatmullRomSpline must be finite. The '
'startHandle given was $startHandle.'
);
assert(
endHandle == null || endHandle.isFinite,
'The provided endHandle of CatmullRomSpline must be finite. The endHandle '
'given was $endHandle.'
);
assert(() {
for (int index = 0; index < controlPoints.length; index++) {
if (!controlPoints[index].isFinite) {
throw FlutterError(
'The provided CatmullRomSpline control point at index $index is not '
'finite. The control point given was ${controlPoints[index]}.'
);
}
}
return true;
}());
// If not specified, select the first and last control points (which are
// handles: they are not intersected by the resulting curve) so that they
// extend the first and last segments, respectively.

View file

@ -305,6 +305,28 @@ void main() {
expect(() {
CatmullRomSpline(const <Offset>[Offset.zero, Offset.zero, Offset.zero, Offset.zero], tension: 2.0);
}, throwsAssertionError);
expect(() {
CatmullRomSpline(
const <Offset>[Offset(double.infinity, 0.0), Offset.zero, Offset.zero, Offset.zero],
).generateSamples();
}, throwsAssertionError);
expect(() {
CatmullRomSpline(
const <Offset>[Offset(0.0, double.infinity), Offset.zero, Offset.zero, Offset.zero],
).generateSamples();
}, throwsAssertionError);
expect(() {
CatmullRomSpline(
startHandle: const Offset(0.0, double.infinity),
const <Offset>[Offset.zero, Offset.zero, Offset.zero, Offset.zero],
).generateSamples();
}, throwsAssertionError);
expect(() {
CatmullRomSpline(
endHandle: const Offset(0.0, double.infinity),
const <Offset>[Offset.zero, Offset.zero, Offset.zero, Offset.zero],
).generateSamples();
}, throwsAssertionError);
});
test('CatmullRomSpline interpolates values properly when precomputed', () {
@ -353,6 +375,24 @@ void main() {
expect(() {
CatmullRomSpline.precompute(const <Offset>[Offset.zero, Offset.zero, Offset.zero, Offset.zero], tension: 2.0);
}, throwsAssertionError);
expect(() {
CatmullRomSpline.precompute(const <Offset>[Offset(double.infinity, 0.0), Offset.zero, Offset.zero, Offset.zero]);
}, throwsAssertionError);
expect(() {
CatmullRomSpline.precompute(const <Offset>[Offset(0.0, double.infinity), Offset.zero, Offset.zero, Offset.zero]);
}, throwsAssertionError);
expect(() {
CatmullRomSpline.precompute(
startHandle: const Offset(0.0, double.infinity),
const <Offset>[Offset.zero, Offset.zero, Offset.zero, Offset.zero],
);
}, throwsAssertionError);
expect(() {
CatmullRomSpline.precompute(
endHandle: const Offset(0.0, double.infinity),
const <Offset>[Offset.zero, Offset.zero, Offset.zero, Offset.zero],
);
}, throwsAssertionError);
});
test('CatmullRomCurve interpolates given points correctly', () {