Guard analogous() and monochromatic() against counts that never terminate - #283
Open
BigAchiever wants to merge 1 commit into
Open
Guard analogous() and monochromatic() against counts that never terminate#283BigAchiever wants to merge 1 commit into
BigAchiever wants to merge 1 commit into
Conversation
…nate
Both functions decrement their loop counter and test it for truthiness:
for (hsl.h = ...; --results; ) // analogous
while (results--) // monochromatic
A counter that never lands exactly on 0 never stops. From -1 the sequence is
-2, -3, -4...; from 1.5 it is 0.5, -0.5, -1.5... Each pass pushes another
colour, so the process exhausts its heap and aborts with exit 134.
$ node --max-old-space-size=256 -e "require('tinycolor2')('red').analogous(-1)"
FATAL ERROR: Reached heap limit Allocation failed
$ echo $?
134
Six cases reproduce: both functions at -1, 1.5 and 0.5.
The reason this survived is that `results = results || 6` looks like a default
but acts as an accidental guard. 0, null, undefined, NaN and false are falsy,
so every value someone would casually test gets replaced by 6. -1, 1.5 and 0.5
are truthy and pass straight through.
polyad() already validates the same shape of argument, so this brings the other
two combination functions in line with it. The guard additionally rejects
non-integers, which polyad does not need: its loop is `for (i = 1; i < number;
i++)` and terminates on fractional counts, whereas these two cannot.
Falsy inputs still fall through to the default of 6, unchanged.
Fixes bgrins#280
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #280.
analogous(results)andmonochromatic(results)both decrement their loop counter and test it for truthiness:A counter that never lands exactly on
0never stops. From-1the sequence is-2, -3, -4…; from1.5it is0.5, -0.5, -1.5…. Each pass pushes another colour, so the process exhausts its heap:Six cases reproduce: both functions at
-1,1.5and0.5.Why it went unnoticed
results = results || 6reads as a default but behaves as an accidental guard.0,null,undefined,NaNandfalseare all falsy, so every value someone would casually reach for gets replaced by6before the loop sees it.-1,1.5and0.5are truthy and pass straight through — so the functions are safe for exactly the inputs you'd test and unsafe for the ones you wouldn't.The change
polyad()already validates the same shape of argument, so this brings the other two combination functions in line with it rather than introducing a new policy.The guard additionally rejects non-integers, which
polyaddoesn't need — its loop isfor (i = 1; i < number; i++)and terminates fine on1.5, whereas--resultscannot.Falsy inputs still fall through to the default of
6, unchanged.analogous(3)still returns 3.I considered coercing instead (
Math.max(1, Math.floor(results))), which would be gentler on any caller currently passing a fraction, but it silently changes their result. Happy to switch if you'd prefer that.Testing
test.jscovering the six throwing inputs and confirming falsy values still default.polyadstill ignored as before.One thing to check
I don't have Deno installed, so I couldn't run
deno task build. I applied the identical guard by hand tonpm/cjs/tinycolor.js,npm/esm/tinycolor.jsandtinycolor.jsso the generated files match what the build would produce — the guard is plain ES5 and shouldn't transpile differently. Worth regenerating them properly before merge.Found while differentially fuzzing a Rust port of TinyColor against the original. The fuzzer never caught this one: for these inputs the original doesn't return a wrong answer, it stops existing, and there's nothing to diff against a dead process.