Program: 15696
Project: 15696
Buggy source:
static Object[] unwrapArguments(List args,Class[] argTypes,boolean isVarargs,BeansWrapper w) throws TemplateModelException {
  if (args == null)   return null;
  int typesLen=argTypes.length;
  int argsLen=args.size();
  Object[] unwrappedArgs=new Object[typesLen];
  Iterator it=args.iterator();
  int normalArgCnt=isVarargs ? typesLen - 1 : typesLen;
  int argIdx=0;
  while (argIdx < normalArgCnt) {
    Class argType=argTypes[argIdx];
    TemplateModel argVal=(TemplateModel)it.next();
    Object unwrappedArgVal=w.tryUnwrap(argVal,argType);
    if (unwrappedArgVal == BeansWrapper.CAN_NOT_UNWRAP) {
      throw createArgumentTypeMismarchException(argIdx,argVal,argType);
    }
    if (unwrappedArgVal == null && argType.isPrimitive()) {
      throw createNullToPrimitiveArgumentException(argIdx,argType);
    }
    unwrappedArgs[argIdx++]=unwrappedArgVal;
  }
  if (isVarargs) {
    Class varargType=argTypes[typesLen - 1];
    Class varargItemType=varargType.getComponentType();
    if (!it.hasNext()) {
      unwrappedArgs[argIdx++]=Array.newInstance(varargItemType,0);
    }
 else {
      TemplateModel argVal=(TemplateModel)it.next();
      Object unwrappedArgVal;
      if (argsLen - argIdx == 1 && (unwrappedArgVal=w.tryUnwrap(argVal,varargType)) != BeansWrapper.CAN_NOT_UNWRAP) {
        unwrappedArgs[argIdx++]=unwrappedArgVal;
      }
 else {
        int varargArrayLen=argsLen - argIdx;
        Object varargArray=Array.newInstance(varargItemType,varargArrayLen);
        for (int varargIdx=0; varargIdx < varargArrayLen; varargIdx++) {
          TemplateModel varargVal=(TemplateModel)(varargIdx == 0 ? argVal : it.next());
          Object unwrappedVarargVal=w.tryUnwrap(varargVal,varargItemType);
          if (unwrappedVarargVal == BeansWrapper.CAN_NOT_UNWRAP) {
            throw createArgumentTypeMismarchException(argIdx + varargIdx,varargVal,varargItemType);
          }
          if (unwrappedVarargVal == null && varargItemType.isPrimitive()) {
            throw createNullToPrimitiveArgumentException(argIdx + varargIdx,varargItemType);
          }
          Array.set(varargArray,varargIdx,unwrappedVarargVal);
        }
        unwrappedArgs[argIdx++]=varargArray;
      }
    }
  }
  return unwrappedArgs;
}

Fixed version (ground truth):
private Object[] unwrapArguments(List args,Class[] argTypes,boolean isVarargs,BeansWrapper w) throws TemplateModelException {
  if (args == null)   return null;
  int typesLen=argTypes.length;
  int argsLen=args.size();
  Object[] unwrappedArgs=new Object[typesLen];
  Iterator it=args.iterator();
  int normalArgCnt=isVarargs ? typesLen - 1 : typesLen;
  int argIdx=0;
  while (argIdx < normalArgCnt) {
    Class argType=argTypes[argIdx];
    TemplateModel argVal=(TemplateModel)it.next();
    Object unwrappedArgVal=w.tryUnwrap(argVal,argType);
    if (unwrappedArgVal == BeansWrapper.CAN_NOT_UNWRAP) {
      throw createArgumentTypeMismarchException(argIdx,argVal,argType);
    }
    if (unwrappedArgVal == null && argType.isPrimitive()) {
      throw createNullToPrimitiveArgumentException(argIdx,argType);
    }
    unwrappedArgs[argIdx++]=unwrappedArgVal;
  }
  if (isVarargs) {
    Class varargType=argTypes[typesLen - 1];
    Class varargItemType=varargType.getComponentType();
    if (!it.hasNext()) {
      unwrappedArgs[argIdx++]=Array.newInstance(varargItemType,0);
    }
 else {
      TemplateModel argVal=(TemplateModel)it.next();
      Object unwrappedArgVal;
      if (argsLen - argIdx == 1 && (unwrappedArgVal=w.tryUnwrap(argVal,varargType)) != BeansWrapper.CAN_NOT_UNWRAP) {
        unwrappedArgs[argIdx++]=unwrappedArgVal;
      }
 else {
        int varargArrayLen=argsLen - argIdx;
        Object varargArray=Array.newInstance(varargItemType,varargArrayLen);
        for (int varargIdx=0; varargIdx < varargArrayLen; varargIdx++) {
          TemplateModel varargVal=(TemplateModel)(varargIdx == 0 ? argVal : it.next());
          Object unwrappedVarargVal=w.tryUnwrap(varargVal,varargItemType);
          if (unwrappedVarargVal == BeansWrapper.CAN_NOT_UNWRAP) {
            throw createArgumentTypeMismarchException(argIdx + varargIdx,varargVal,varargItemType);
          }
          if (unwrappedVarargVal == null && varargItemType.isPrimitive()) {
            throw createNullToPrimitiveArgumentException(argIdx + varargIdx,varargItemType);
          }
          Array.set(varargArray,varargIdx,unwrappedVarargVal);
        }
        unwrappedArgs[argIdx++]=varargArray;
      }
    }
  }
  return unwrappedArgs;
}