Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add protections against infinite loop in bezier calculations #27756

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/matplotlib/bezier.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,17 @@ def find_bezier_t_intersecting_with_closedpath(

if start_inside ^ middle_inside:
t1 = middle_t
if end == middle:
# Edge case where infinite loop is possible
# Caused by large numbers relative to tolerance
return t0, t1
end = middle
else:
t0 = middle_t
if start == middle:
# Edge case where infinite loop is possible
# Caused by large numbers relative to tolerance
return t0, t1
start = middle
start_inside = middle_inside

Expand Down
17 changes: 17 additions & 0 deletions lib/matplotlib/tests/test_bezier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
Tests specific to the bezier module.
"""

from matplotlib.bezier import inside_circle, split_bezier_intersecting_with_closedpath


def test_split_bezier_with_large_values():
# These numbers come from gh-27753
arrow_path = [(96950809781500.0, 804.7503795623779),
(96950809781500.0, 859.6242585800646),
(96950809781500.0, 914.4981375977513)]
in_f = inside_circle(96950809781500.0, 804.7503795623779, 0.06)
split_bezier_intersecting_with_closedpath(arrow_path, in_f)
# All we are testing is that this completes
# The failure case is an infinite loop resulting from floating point precision
# pytest will timeout if that occurs