Skip to content

Commit

Permalink
Assert against infinite values of control points in CatmullRomSpline …
Browse files Browse the repository at this point in the history
…(#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 flutter/flutter#131246
  • Loading branch information
Piinks authored Aug 3, 2023
1 parent 0ad45f2 commit b3a4dec
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/flutter/lib/src/animation/curves.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions packages/flutter/test/animation/curves_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand Down Expand Up @@ -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', () {
Expand Down

0 comments on commit b3a4dec

Please sign in to comment.