Menu

#1136 ECAMScript: NullPointerException in getLeft() and getRight()

PMD-5.1.0
closed
PMD
3-Major
Bug
5.0.5
2015-02-13
2013-10-08
No

Hello,

In some circumstances call to node.getLeft() or node.getRight() generated a NullPointerException.

JavaScript example used to make tests:

function test(){
  a();      // OK
  b.c();    // OK
  d[0]();   // OK
  e[0].f(); // OK
  y.z[0](); // FAIL ==> java.lang.NullPointerException
}

Java code used to analyse the javascript code:

package com.home.cwcodecheck.pmd;

import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ecmascript.ast.ASTElementGet;
import net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionCall;
import net.sourceforge.pmd.lang.ecmascript.ast.ASTName;
import net.sourceforge.pmd.lang.ecmascript.ast.ASTPropertyGet;
import net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule;

public class DebugPMD extends AbstractEcmascriptRule {

public Object visit(final ASTFunctionCall node, final Object data) {
    System.out.println("call: " + getName(node.getTarget()) + "()");
    return super.visit(node, data);
}

protected String getName(final Node node){

    if( node instanceof ASTName ){
        return ((ASTName)node).getIdentifier();
    }

    if( node instanceof ASTPropertyGet ){
        final ASTPropertyGet pgNode = (ASTPropertyGet)node;
        final String leftName  = getName(pgNode.**getLeft()**);
        final String rightName = getName(pgNode.**getRight()**);
        return leftName + "." + rightName;
    }

    if( node instanceof ASTElementGet ){
        return getName(((ASTElementGet)node).getTarget()) + "[]";
    }

    return "????";

}

}

Output:

call: a()
call: b.c()
call: d[]()
call: e[].f()

java.lang.NullPointerException
at net.sourceforge.pmd.lang.ast.AbstractNode.jjtGetChild(AbstractNode.java:77)
at net.sourceforge.pmd.lang.ecmascript.ast.AbstractInfixEcmascriptNode.getLeft(AbstractInfixEcmascriptNode.java:23)
at com.home.cwcodecheck.pmd.DebugPMD.getName(DebugPMD.java:31)
at com.home.cwcodecheck.pmd.DebugPMD.getName(DebugPMD.java:38)
at com.home.cwcodecheck.pmd.DebugPMD.visit(DebugPMD.java:17)
at net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionCall.jjtAccept(ASTFunctionCall.java:17)
at net.sourceforge.pmd.lang.ecmascript.ast.AbstractEcmascriptNode.childrenAccept(AbstractEcmascriptNode.java:48)
at net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule.visit(AbstractEcmascriptRule.java:105)
at net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule.visit(AbstractEcmascriptRule.java:166)
at net.sourceforge.pmd.lang.ecmascript.ast.ASTExpressionStatement.jjtAccept(ASTExpressionStatement.java:19)
at net.sourceforge.pmd.lang.ecmascript.ast.AbstractEcmascriptNode.childrenAccept(AbstractEcmascriptNode.java:48)
at net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule.visit(AbstractEcmascriptRule.java:105)
at net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule.visit(AbstractEcmascriptRule.java:130)
at net.sourceforge.pmd.lang.ecmascript.ast.ASTBlock.jjtAccept(ASTBlock.java:18)
at net.sourceforge.pmd.lang.ecmascript.ast.AbstractEcmascriptNode.childrenAccept(AbstractEcmascriptNode.java:48)
at net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule.visit(AbstractEcmascriptRule.java:105)
at net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule.visit(AbstractEcmascriptRule.java:182)
at net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionNode.jjtAccept(ASTFunctionNode.java:18)

In place of the java stack dump, the expected result is
call: y.z[]();

NOTE: The problem appears during the getLeft() because it is called before getRight(). But there is the same problem if getRight() is called first.

Regards

Discussion

  • Jimmy Profit

    Jimmy Profit - 2013-10-09

    Hi,

    In fact the problem is not in getLeft() ot getRight() but in pmd.lang.ecmascript.ASTElementGet.getTarget() and getElement(). (These functions have been changed to fix bug [#1118])

    In some circumstances (or always?) these functions return an ASTPropertyGet node with no children.
    If you try to use the children of this invalid node, for example by calling getLeft() or getRight(), you get the NullPointerException because the children array is null.

    It is obvious that the problem is in the function EcmascriptTreeBuilder.createNodeAdapter(), but my understanding of the PMD internals stops here.

    Regards

     

    Related

    Issues: #1118


    Last edit: Jimmy Profit 2013-10-09
  • Andreas Dangel

    Andreas Dangel - 2013-10-10
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: PMD-5.0.x --> PMD-5.1.0
     

Log in to post a comment.

MongoDB Logo MongoDB