Minifier optimizer bug?

Sep 24, 2012 at 11:21 PM

I came across an issue with the minifier optimization or perhaps it's a JavaScript bug. At one point in our code, we have the following scenario:

            enterElements: function () {
                var count = 1;
                for (var i in _elems) {
                    var e = _getElementById(i);

                    // Hide everything
                   
                    e.style.left = _container.offsetWidth + "px";
                    e.style.width = "0px";
                    e.style.height = "0px";
                    e.style.top = _carousel.offsetHeight / 2 + "px";

                    // Animate the visible elements in one by one
                    var c = parseInt(_class.getClass(e));
                    var o = 0;

                    if (c >= 0 && c <= 4) {
                        o = _einfo[c].o;
                        _css.setTransition(e, "all .8s cubic-bezier(.1,.9,.2,1)");
                    }
                    else {
                        _css.setTransition(e, "");
                    }

                    setTimeout(function (e, c, o) {
                        return function () { try { _css.setCss(e, _einfo[c].y, _einfo[c].x, _einfo[c].w, _einfo[c].h, o, _einfo[c].z); _animations.fadeContents(e); } catch (e) { } }
                    }(e, c, o), c * 100 + 150);                                                  
                }

            },

Here is the minified version (re-beautified). I’ve highlighted where we would get the null reference. The value for i can be NaN so the index fails. In regular unit testing the issue never came up, since NaN does not fit into the values 0, 1, 2, 3, 4.

                enterElements: function() {
                    var f = 1,
                        u, t, i, r;
                    for (u in s)
                            t = ht(u),
                            t.style.left = c.offsetWidth + "px",
                            t.style.width = "0px",
                            t.style.height = "0px", t.style.top = e.offsetHeight / 2 + "px",
                            i = vt(y.getClass(t), 0),  // Can return NaN.
                            r = 0,
                            i < 0 || i > 4 ? o.setTransition(t, "") : (r = n[i].o, o.setTransition(t, "all .8s cubic-bezier(.1,.9,.2,1)")),
                            setTimeout(function (t, i, r) {
                                return function() {
                                    try {
                                        o.setCss(t, n[i].y, n[i].x, n[i].w, n[i].h, r, n[i].z), b.fadeContents(t)
                                    } catch (t) {}
                                }
                        }(t, i, r), i * 100 + 150)
                },

 

Coordinator
Sep 25, 2012 at 1:02 AM

Yeah, that looks like a bug in the minifier. In this part of the source code:

    if (c >= 0 && c <= 4) {
        o = _einfo[c].o;
          _css.setTransition(e, "all .8s cubic-bezier(.1,.9,.2,1)");
    }
     else {
         _css.setTransition(e, "");
     }

if c equals NaN, the else-clause is executed. But it's getting minified to:

      i < 0 || i > 4 ? o.setTransition(t, "") : (r = n[i].o, o.setTransition(t, "all .8s cubic-bezier(.1,.9,.2,1)"))

When i (minified name of "c") equals NaN in the minified code, the false-expression is executed, which is not right. The minifier negated the condition and flipped the true/false branches -- which in this case, is not the same as the original. Good find!

Coordinator
Sep 25, 2012 at 1:17 AM

Which version of AjaxMin are you using, just out of curiosity? I ask because I went to fix this, but the current version I'm working on right now doesn't seem to repro.

Coordinator
Sep 25, 2012 at 1:30 AM

I am getting old and rusty. Apparently this already came up as issue #17256 back in January, and I fixed it in version 4.44. Please upgrade to the latest version and this problem should go away. :)

Sep 25, 2012 at 7:46 PM

Thanks Ron. I just joined my team and came across this bug. I will find out how to get AjaxMin updated. I don’t want anyone else to have to go through the pain of debugging our code when it’s already been fixed.

From: ronlo [email removed]
Sent: Monday, September 24, 2012 5:31 PM
To: bradt@msn.com
Subject: Re: Minifier optimizer bug? [ajaxmin:396736]

From: ronlo

I am getting old and rusty. Apparently this already came up as issue #17256 back in January, and I fixed it in version 4.44. Please upgrade to the latest version and this problem should go away. :)