2

An exception occurred several times inside the following function, running in the (Chrome) browsers of our users. NB the code is minified, I've just added newlines for readabilily.

  function a(c, b, e, f) {
    for (var d in b) {
      if (c && _.isObject(b[d]) === true && _.isArray(b[d]) === false) {
        a(c[d], b[d], e, d + ".")
      } else {
        if (!c || _.isEqual(c[d], b[d]) === false) {
          e.push({
            name: f + d,
            value: b[d]
          })
        }
      }
    }
    return e
  }

The exception was: TypeError: Cannot read property 'beginning' of null

So far my analysis is this:

  1. Known fact: beginning is one of the values of the d variable (a property of b).
  2. So, the exception occurred when evaluating b[d]or c[d]
  3. Inside the for loop, c can be null, but not b (b null => no loop)
  4. So, the only way to trigger that exception is to have a null c
  5. If c is null, we have to take the else path
  6. If c is null, !c is truthy, so _.isEqual(...) should not be evaluated (see Is that js expression safe: if( !x || doSomething( x[prop], y[prop] ) === false )).

At that point, I've reached a dead end in my reasoning. The exception should not happen. I probably made a mistake somewhere along the line, but where ?

NB: the problem seems to be fixed (I can't reproduce it, so I can't be sure), just by changing the code a bit, adding a separate 'is c null ?' test before the if...else, but that's not very satisfying.

Community
  • 1
  • 1
Ilya
  • 5,377
  • 2
  • 18
  • 33
  • Add console lines to get a better idea on where it is failing. – epascarello Dec 18 '15 at 21:00
  • Have some sort of console through which you can track where it breaks exactly. It'll be easier to debug. – myselfmiqdad Dec 18 '15 at 21:04
  • 1
    the issue is with `c[d]`, after the first recursion, which is assumed to be something, but it's not always something just because `b[d]` is something – dandavis Dec 18 '15 at 21:07
  • @dandavis see point 5 and 6. Don't you agree ? – Ilya Dec 18 '15 at 21:10
  • 1
    @epascarello: I don't have access to the user's browser, only the stack trace of the exception logged on the server. – Ilya Dec 18 '15 at 21:12
  • is c a boolean? , if c IS a boolean and its null it will go to the else path and when its called in the other if it wont work since !c is calling a null not false or true, doesnt it works like that? – ivan Dec 18 '15 at 21:21
  • @ivan c is not a boolean in the first `a()` call, but even if it was at some point, I think the question is the same. – Ilya Dec 18 '15 at 21:47
  • read this: http://stackoverflow.com/questions/11005286/check-if-null-boolean-is-true-results-in-exception – ivan Dec 18 '15 at 22:24
  • 1
    @ivan this is javascript, not java – Ilya Dec 19 '15 at 09:08

0 Answers0