bezierjs icon indicating copy to clipboard operation
bezierjs copied to clipboard

function "getABC()" has potential for divide by 0 error

Open distinctdan opened this issue 5 years ago • 3 comments

I was looking through the codebase, and it looks like this function has the potential for a divide by 0 error if s is 0.

function getABC(n, S, B, E, t) {
    if (typeof t === "undefined") {
      t = 0.5;
    }
    var u = utils.projectionratio(t, n),
      um = 1 - u,
      C = {
        x: u * S.x + um * E.x,
        y: u * S.y + um * E.y
      },
      s = utils.abcratio(t, n),
      A = {
        x: B.x + (B.x - C.x) / s,
        y: B.y + (B.y - C.y) / s
      };
    return { A: A, B: B, C: C };
  }

distinctdan avatar Feb 11 '19 23:02 distinctdan

If we're code hunting: while true, can you find an actual curve (or set of curves) for which this would actually happen?

Pomax avatar Feb 12 '19 01:02 Pomax

Looking at utils.abcratio, I see it returns t if t === 0, so this function would error if you ever pass it 0 for t.

distinctdan avatar Feb 12 '19 20:02 distinctdan

As an isolated case that'd be ridiculous, but as part of a loop/map that could certainly happen. Adding t=0 and t=1 shortcuts is a good idea.

Pomax avatar Feb 14 '19 16:02 Pomax