Uploaded image for project: 'Commons JEXL'
  1. Commons JEXL
  2. JEXL-321

Empty do-while loop is broken

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.1
    • 3.2
    • None

    Description

      The following test case with AIOOB.

          @Test
          public void testEmptyBody() throws Exception {
              JexlScript e = JEXL.createScript("var i = 0; do ; while((i+=1) < 10); i");
              JexlContext jc = new MapContext();
              Object o = e.execute(jc);
              Assert.assertEquals(10, o);       
          } 

      The suggestion is to change interpreter as follows

          @Override
          protected Object visit(ASTDoWhileStatement node, Object data) {
              Object result = null;
              /* last objectNode is the expression */
              Node expressionNode = node.jjtGetChild(node.jjtGetNumChildren()-1);
              do {
                  cancelCheck(node);
                  if (node.jjtGetNumChildren() > 1) {
                      try {
                          // execute statement
                          result = node.jjtGetChild(0).jjtAccept(this, data);
                      } catch (JexlException.Break stmtBreak) {
                          break;
                      } catch (JexlException.Continue stmtContinue) {
                          //continue;
                      }
                  }
              } while (arithmetic.toBoolean(expressionNode.jjtAccept(this, data)));
              return result;
          }
      

      and Debugger as follows

          @Override
          protected Object visit(ASTDoWhileStatement node, Object data) {
              int num = node.jjtGetNumChildren();
              builder.append("do ");
              if (num > 1) {
                  acceptStatement(node.jjtGetChild(0), data);
              } else {
                  builder.append(" ; ");
              }
              builder.append(" while (");
              accept(node.jjtGetChild(num - 1), data);
              builder.append(")");
              return data;
          }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            dmitri_blinov Dmitri Blinov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: