Program: 15672
Project: 15672
Buggy source:
private Expression readTerm(){
  Expression r;
switch (currentTokenType) {
case AT:
    read();
  r=new Variable(session,readAliasIdentifier());
if (readIf("String_Node_Str")) {
  Expression value=readExpression();
  Function function=Function.getFunction(database,"String_Node_Str");
  function.setParameter(0,r);
  function.setParameter(1,value);
  r=function;
}
break;
case PARAMETER:
boolean indexed=Character.isDigit(sqlCommandChars[parseIndex]);
read();
Parameter p;
if (indexed && currentTokenType == VALUE && currentValue.getType() == Value.INT) {
if (indexedParameterList == null) {
if (parameters == null) {
throw getSyntaxError();
}
 else if (parameters.size() > 0) {
throw DbException.get(ErrorCode.CANNOT_MIX_INDEXED_AND_UNINDEXED_PARAMS);
}
indexedParameterList=New.arrayList();
}
int index=currentValue.getInt() - 1;
if (index < 0 || index >= Constants.MAX_PARAMETER_INDEX) {
throw DbException.getInvalidValueException("String_Node_Str",index);
}
if (indexedParameterList.size() <= index) {
indexedParameterList.ensureCapacity(index + 1);
while (indexedParameterList.size() <= index) {
indexedParameterList.add(null);
}
}
p=indexedParameterList.get(index);
if (p == null) {
p=new Parameter(index);
indexedParameterList.set(index,p);
}
read();
}
 else {
if (indexedParameterList != null) {
throw DbException.get(ErrorCode.CANNOT_MIX_INDEXED_AND_UNINDEXED_PARAMS);
}
p=new Parameter(parameters.size());
}
parameters.add(p);
r=p;
break;
case KEYWORD:
if (isToken("String_Node_Str") || isToken("String_Node_Str") || isToken("String_Node_Str")) {
Query query=parseSelect();
r=new Subquery(query);
}
 else {
throw getSyntaxError();
}
break;
case IDENTIFIER:
String name=currentToken;
if (currentTokenQuoted) {
read();
if (readIf("String_Node_Str")) {
r=readFunction(null,name);
}
 else if (readIf("String_Node_Str")) {
r=readTermObjectDot(name);
}
 else {
r=new ExpressionColumn(database,null,null,name);
}
}
 else {
read();
if (readIf("String_Node_Str")) {
r=readTermObjectDot(name);
}
 else if (equalsToken("String_Node_Str",name)) {
r=readCase();
}
 else if (readIf("String_Node_Str")) {
r=readFunction(null,name);
}
 else if (equalsToken("String_Node_Str",name)) {
r=readFunctionWithoutParameters("String_Node_Str");
}
 else if (equalsToken("String_Node_Str",name)) {
if (readIf("String_Node_Str")) {
r=readFunctionWithoutParameters("String_Node_Str");
}
 else if (readIf("String_Node_Str")) {
r=readFunctionWithoutParameters("String_Node_Str");
}
 else if (readIf("String_Node_Str")) {
r=readFunctionWithoutParameters("String_Node_Str");
}
 else {
r=new ExpressionColumn(database,null,null,name);
}
}
 else if (equalsToken("String_Node_Str",name) && readIf("String_Node_Str")) {
read("String_Node_Str");
Sequence sequence=readSequence();
r=new SequenceValue(sequence);
}
 else if (currentTokenType == VALUE && currentValue.getType() == Value.STRING) {
if (equalsToken("String_Node_Str",name) || equalsToken("String_Node_Str",name)) {
String date=currentValue.getString();
read();
r=ValueExpression.get(ValueDate.parse(date));
}
 else if (equalsToken("String_Node_Str",name) || equalsToken("String_Node_Str",name)) {
String time=currentValue.getString();
read();
r=ValueExpression.get(ValueTime.parse(time));
}
 else if (equalsToken("String_Node_Str",name) || equalsToken("String_Node_Str",name)) {
String timestamp=currentValue.getString();
read();
r=ValueExpression.get(ValueTimestamp.parse(timestamp,session.getDatabase().getMode()));
}
 else if (equalsToken("String_Node_Str",name)) {
read();
byte[] buffer=StringUtils.convertHexToBytes(currentValue.getString());
r=ValueExpression.get(ValueBytes.getNoCopy(buffer));
}
 else if (equalsToken("String_Node_Str",name)) {
String text=currentValue.getString();
text=StringUtils.replaceAll(text,"String_Node_Str","String_Node_Str");
read();
r=ValueExpression.get(ValueString.get(text));
}
 else if (equalsToken("String_Node_Str",name)) {
String text=currentValue.getString();
read();
r=ValueExpression.get(ValueString.get(text));
}
 else {
r=new ExpressionColumn(database,null,null,name);
}
}
 else {
r=new ExpressionColumn(database,null,null,name);
}
}
break;
case MINUS:
read();
if (currentTokenType == VALUE) {
r=ValueExpression.get(currentValue.negate());
if (r.getType() == Value.LONG && r.getValue(session).getLong() == Integer.MIN_VALUE) {
r=ValueExpression.get(ValueInt.get(Integer.MIN_VALUE));
}
 else if (r.getType() == Value.DECIMAL && r.getValue(session).getBigDecimal().compareTo(ValueLong.MIN_BD) == 0) {
r=ValueExpression.get(ValueLong.get(Long.MIN_VALUE));
}
read();
}
 else {
r=new Operation(Operation.NEGATE,readTerm(),null);
}
break;
case PLUS:
read();
r=readTerm();
break;
case OPEN:
read();
if (readIf("String_Node_Str")) {
r=new ExpressionList(new Expression[0]);
}
 else {
r=readExpression();
if (readIf("String_Node_Str")) {
ArrayList<Expression> list=New.arrayList();
list.add(r);
while (!readIf("String_Node_Str")) {
r=readExpression();
list.add(r);
if (!readIf("String_Node_Str")) {
read("String_Node_Str");
break;
}
}
Expression[] array=new Expression[list.size()];
list.toArray(array);
r=new ExpressionList(array);
}
 else {
read("String_Node_Str");
}
}
break;
case TRUE:
read();
r=ValueExpression.get(ValueBoolean.get(true));
break;
case FALSE:
read();
r=ValueExpression.get(ValueBoolean.get(false));
break;
case CURRENT_TIME:
read();
r=readFunctionWithoutParameters("String_Node_Str");
break;
case CURRENT_DATE:
read();
r=readFunctionWithoutParameters("String_Node_Str");
break;
case CURRENT_TIMESTAMP:
{
Function function=Function.getFunction(database,"String_Node_Str");
read();
if (readIf("String_Node_Str")) {
if (!readIf("String_Node_Str")) {
function.setParameter(0,readExpression());
read("String_Node_Str");
}
}
function.doneWithParameters();
r=function;
break;
}
case ROWNUM:
read();
if (readIf("String_Node_Str")) {
read("String_Node_Str");
}
if (currentSelect == null && currentPrepared == null) {
throw getSyntaxError();
}
r=new Rownum(currentSelect == null ? currentPrepared : currentSelect);
break;
case NULL:
read();
r=ValueExpression.getNull();
break;
case VALUE:
r=ValueExpression.get(currentValue);
read();
break;
default :
throw getSyntaxError();
}
if (readIf("String_Node_Str")) {
Function function=Function.getFunction(database,"String_Node_Str");
function.setParameter(0,r);
r=readExpression();
r=new Operation(Operation.PLUS,r,ValueExpression.get(ValueInt.get(1)));
function.setParameter(1,r);
r=function;
read("String_Node_Str");
}
if (readIf("String_Node_Str")) {
if (isToken("String_Node_Str")) {
read("String_Node_Str");
read("String_Node_Str");
}
if (readIf("String_Node_Str")) {
FunctionAlias f=findFunctionAlias(Constants.SCHEMA_MAIN,"String_Node_Str");
if (f == null) {
throw getSyntaxError();
}
Expression[] args={r};
JavaFunction func=new JavaFunction(f,args);
r=func;
}
 else {
Column col=parseColumnWithType(null);
Function function=Function.getFunction(database,"String_Node_Str");
function.setDataType(col);
function.setParameter(0,r);
r=function;
}
}
return r;
}

Fixed version (ground truth):
private Expression readTerm(){
  Expression r;
switch (currentTokenType) {
case AT:
    read();
  r=new Variable(session,readAliasIdentifier());
if (readIf("String_Node_Str")) {
  Expression value=readExpression();
  Function function=Function.getFunction(database,"String_Node_Str");
  function.setParameter(0,r);
  function.setParameter(1,value);
  r=function;
}
break;
case PARAMETER:
boolean indexed=Character.isDigit(sqlCommandChars[parseIndex]);
Parameter p;
if (indexed) {
readParameterIndex();
if (indexedParameterList == null) {
if (parameters == null) {
throw getSyntaxError();
}
 else if (parameters.size() > 0) {
throw DbException.get(ErrorCode.CANNOT_MIX_INDEXED_AND_UNINDEXED_PARAMS);
}
indexedParameterList=New.arrayList();
}
int index=currentValue.getInt() - 1;
if (index < 0 || index >= Constants.MAX_PARAMETER_INDEX) {
throw DbException.getInvalidValueException("String_Node_Str",index);
}
if (indexedParameterList.size() <= index) {
indexedParameterList.ensureCapacity(index + 1);
while (indexedParameterList.size() <= index) {
indexedParameterList.add(null);
}
}
p=indexedParameterList.get(index);
if (p == null) {
p=new Parameter(index);
indexedParameterList.set(index,p);
}
read();
}
 else {
read();
if (indexedParameterList != null) {
throw DbException.get(ErrorCode.CANNOT_MIX_INDEXED_AND_UNINDEXED_PARAMS);
}
p=new Parameter(parameters.size());
}
parameters.add(p);
r=p;
break;
case KEYWORD:
if (isToken("String_Node_Str") || isToken("String_Node_Str") || isToken("String_Node_Str")) {
Query query=parseSelect();
r=new Subquery(query);
}
 else {
throw getSyntaxError();
}
break;
case IDENTIFIER:
String name=currentToken;
if (currentTokenQuoted) {
read();
if (readIf("String_Node_Str")) {
r=readFunction(null,name);
}
 else if (readIf("String_Node_Str")) {
r=readTermObjectDot(name);
}
 else {
r=new ExpressionColumn(database,null,null,name);
}
}
 else {
read();
if (readIf("String_Node_Str")) {
r=readTermObjectDot(name);
}
 else if (equalsToken("String_Node_Str",name)) {
r=readCase();
}
 else if (readIf("String_Node_Str")) {
r=readFunction(null,name);
}
 else if (equalsToken("String_Node_Str",name)) {
r=readFunctionWithoutParameters("String_Node_Str");
}
 else if (equalsToken("String_Node_Str",name)) {
if (readIf("String_Node_Str")) {
r=readFunctionWithoutParameters("String_Node_Str");
}
 else if (readIf("String_Node_Str")) {
r=readFunctionWithoutParameters("String_Node_Str");
}
 else if (readIf("String_Node_Str")) {
r=readFunctionWithoutParameters("String_Node_Str");
}
 else {
r=new ExpressionColumn(database,null,null,name);
}
}
 else if (equalsToken("String_Node_Str",name) && readIf("String_Node_Str")) {
read("String_Node_Str");
Sequence sequence=readSequence();
r=new SequenceValue(sequence);
}
 else if (currentTokenType == VALUE && currentValue.getType() == Value.STRING) {
if (equalsToken("String_Node_Str",name) || equalsToken("String_Node_Str",name)) {
String date=currentValue.getString();
read();
r=ValueExpression.get(ValueDate.parse(date));
}
 else if (equalsToken("String_Node_Str",name) || equalsToken("String_Node_Str",name)) {
String time=currentValue.getString();
read();
r=ValueExpression.get(ValueTime.parse(time));
}
 else if (equalsToken("String_Node_Str",name) || equalsToken("String_Node_Str",name)) {
String timestamp=currentValue.getString();
read();
r=ValueExpression.get(ValueTimestamp.parse(timestamp,session.getDatabase().getMode()));
}
 else if (equalsToken("String_Node_Str",name)) {
read();
byte[] buffer=StringUtils.convertHexToBytes(currentValue.getString());
r=ValueExpression.get(ValueBytes.getNoCopy(buffer));
}
 else if (equalsToken("String_Node_Str",name)) {
String text=currentValue.getString();
text=StringUtils.replaceAll(text,"String_Node_Str","String_Node_Str");
read();
r=ValueExpression.get(ValueString.get(text));
}
 else if (equalsToken("String_Node_Str",name)) {
String text=currentValue.getString();
read();
r=ValueExpression.get(ValueString.get(text));
}
 else {
r=new ExpressionColumn(database,null,null,name);
}
}
 else {
r=new ExpressionColumn(database,null,null,name);
}
}
break;
case MINUS:
read();
if (currentTokenType == VALUE) {
r=ValueExpression.get(currentValue.negate());
if (r.getType() == Value.LONG && r.getValue(session).getLong() == Integer.MIN_VALUE) {
r=ValueExpression.get(ValueInt.get(Integer.MIN_VALUE));
}
 else if (r.getType() == Value.DECIMAL && r.getValue(session).getBigDecimal().compareTo(ValueLong.MIN_BD) == 0) {
r=ValueExpression.get(ValueLong.get(Long.MIN_VALUE));
}
read();
}
 else {
r=new Operation(Operation.NEGATE,readTerm(),null);
}
break;
case PLUS:
read();
r=readTerm();
break;
case OPEN:
read();
if (readIf("String_Node_Str")) {
r=new ExpressionList(new Expression[0]);
}
 else {
r=readExpression();
if (readIf("String_Node_Str")) {
ArrayList<Expression> list=New.arrayList();
list.add(r);
while (!readIf("String_Node_Str")) {
r=readExpression();
list.add(r);
if (!readIf("String_Node_Str")) {
read("String_Node_Str");
break;
}
}
Expression[] array=new Expression[list.size()];
list.toArray(array);
r=new ExpressionList(array);
}
 else {
read("String_Node_Str");
}
}
break;
case TRUE:
read();
r=ValueExpression.get(ValueBoolean.get(true));
break;
case FALSE:
read();
r=ValueExpression.get(ValueBoolean.get(false));
break;
case CURRENT_TIME:
read();
r=readFunctionWithoutParameters("String_Node_Str");
break;
case CURRENT_DATE:
read();
r=readFunctionWithoutParameters("String_Node_Str");
break;
case CURRENT_TIMESTAMP:
{
Function function=Function.getFunction(database,"String_Node_Str");
read();
if (readIf("String_Node_Str")) {
if (!readIf("String_Node_Str")) {
function.setParameter(0,readExpression());
read("String_Node_Str");
}
}
function.doneWithParameters();
r=function;
break;
}
case ROWNUM:
read();
if (readIf("String_Node_Str")) {
read("String_Node_Str");
}
if (currentSelect == null && currentPrepared == null) {
throw getSyntaxError();
}
r=new Rownum(currentSelect == null ? currentPrepared : currentSelect);
break;
case NULL:
read();
r=ValueExpression.getNull();
break;
case VALUE:
r=ValueExpression.get(currentValue);
read();
break;
default :
throw getSyntaxError();
}
if (readIf("String_Node_Str")) {
Function function=Function.getFunction(database,"String_Node_Str");
function.setParameter(0,r);
r=readExpression();
r=new Operation(Operation.PLUS,r,ValueExpression.get(ValueInt.get(1)));
function.setParameter(1,r);
r=function;
read("String_Node_Str");
}
if (readIf("String_Node_Str")) {
if (isToken("String_Node_Str")) {
read("String_Node_Str");
read("String_Node_Str");
}
if (readIf("String_Node_Str")) {
FunctionAlias f=findFunctionAlias(Constants.SCHEMA_MAIN,"String_Node_Str");
if (f == null) {
throw getSyntaxError();
}
Expression[] args={r};
JavaFunction func=new JavaFunction(f,args);
r=func;
}
 else {
Column col=parseColumnWithType(null);
Function function=Function.getFunction(database,"String_Node_Str");
function.setDataType(col);
function.setParameter(0,r);
r=function;
}
}
return r;
}