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 limit to getDuration() in spring function to avoid infinite loops #798

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ function parseEasingParameters(string) {
return match ? match[1].split(',').map(p => parseFloat(p)) : [];
}

// Spring solver inspired by Webkit Copyright © 2016 Apple Inc. All rights reserved. https://webkit.org/demos/spring/spring.js
/**
* The number of iterations before a loop can be counted as an infinite loop
*/
const INFINITE_LOOP_LIMIT = 10000;

// Spring solver inspired by Webkit Copyright © 2016 Apple Inc. All rights reserved. https://webkit.org/demos/spring/spring.js
function spring(string, duration) {

const params = parseEasingParameters(string);
Expand All @@ -96,14 +100,17 @@ function spring(string, duration) {
if (t === 0 || t === 1) return t;
return 1 - progress;
}

function getDuration() {
const cached = cache.springs[string];
if (cached) return cached;
const frame = 1/6;
let elapsed = 0;
let rest = 0;
while(true) {
let count = 0;

// To avoid infinite loops, stop loop when count reaches `INFINITE_LOOP_LIMIT`
while(++count < INFINITE_LOOP_LIMIT) {
elapsed += frame;
if (solver(elapsed) === 1) {
rest++;
Expand Down