Program: 53288
Project: 53288
Buggy source:
/** 
 * Mandles client request.
 * @param buff Received data from the client.
 * @return Response to the request.        Response is null if request packet is not compleate.
 */
public final byte[] handleRequest(byte[] buff){
  if (buff == null) {
    return null;
  }
  if (!Initialized) {
    throw new RuntimeException("String_Node_Str");
  }
  int[] command=new int[1];
  try {
    byte[] data;
    if (ReceivedFrame.size() != 0) {
      ReceivedFrame.write(buff);
      data=ReceivedFrame.toByteArray();
    }
 else {
      data=buff;
    }
    if (m_Base.getServerID() == null) {
      Object[] sid=new Object[1], cid=new Object[1];
      getAddress(data,cid,sid);
      for (      Object it : this.getServerIDs()) {
        if (((Number)sid[0]).intValue() == ((Number)it).intValue()) {
          m_Base.setServerID(sid[0]);
          m_Base.setClientID(cid[0]);
          break;
        }
      }
      if (m_Base.getServerID() == null) {
        invalidConnection(new ConnectionEventArgs(sid));
        return null;
      }
    }
    if (!m_Base.isDLMSPacketComplete(data)) {
      if (ReceivedFrame.size() == 0) {
        ReceivedFrame.write(buff);
      }
      return null;
    }
    List<Object> names=new ArrayList<Object>();
    GXDLMSObject item=null;
    ByteArrayOutputStream tmp=new ByteArrayOutputStream();
    byte[] frame=new byte[1];
    java.util.Set<RequestTypes> ret=m_Base.getDataFromPacket(data,tmp,frame,command);
    byte[] allData=tmp.toByteArray();
    ReceivedFrame.reset();
    if (ret.contains(RequestTypes.FRAME)) {
      ++FrameIndex;
      if ((frame[0] & 0x1) == 0) {
        if (command[0] != 0) {
          LastCommand=command[0];
        }
        ReceivedData.write(allData);
        SendData.clear();
        FrameIndex=0;
        --m_Base.expectedFrame;
        return m_Base.receiverReady(RequestTypes.FRAME);
      }
 else       if (FrameIndex >= SendData.size() && (frame[0] & 0x1) == 1) {
        SendData.clear();
        FrameIndex=0;
        return m_Base.addFrame(m_Base.generateAliveFrame(),false,(byte[])null,0,0);
      }
      return SendData.get(FrameIndex);
    }
 else     if (ret.contains(RequestTypes.DATABLOCK)) {
      if ((frame[0] & 0x1) == 0) {
        if (command[0] != 0) {
          LastCommand=command[0];
        }
        ReceivedData.write(allData);
        SendData.clear();
        FrameIndex=0;
        return m_Base.receiverReady(RequestTypes.DATABLOCK);
      }
      ++FrameIndex;
      int[] index=new int[1];
      int BlockIndex=(int)GXCommon.getUInt32(allData,index);
      return SendData.get(FrameIndex);
    }
    if (ReceivedData.size() != 0) {
      ReceivedData.write(allData);
      allData=ReceivedData.toByteArray();
      ReceivedData.reset();
      command[0]=LastCommand;
    }
    FrameIndex=0;
    SendData.clear();
    if (command[0] == Command.GloGetRequest.getValue() || command[0] == Command.GloSetRequest.getValue() || command[0] == Command.GloMethodRequest.getValue()) {
      Command[] cmd=new Command[1];
      allData=m_Base.decrypt(allData,cmd);
      command[0]=cmd[0].getValue();
    }
    if (command[0] == Command.Snrm.getValue()) {
      SendData.add(handleSnrmRequest());
      return SendData.get(FrameIndex);
    }
 else     if (command[0] == Command.Aarq.getValue()) {
      SendData.add(handleAARQRequest(data));
      return SendData.get(FrameIndex);
    }
 else     if (command[0] == Command.DisconnectRequest.getValue()) {
      SendData.add(generateDisconnectRequest());
      return SendData.get(FrameIndex);
    }
 else     if (command[0] == Command.WriteRequest.getValue()) {
      int attributeIndex;
      int tmp2[]=new int[1];
      Object[] value=new Object[1];
      ObjectType[] type=new ObjectType[1];
      int[] selector=new int[1];
      getCommand(command[0],allData,type,names,tmp2,selector,value);
      int sn=(int)names.get(0);
      for (      Map.Entry<Integer,GXDLMSObject> it : SortedItems.entrySet()) {
        int aCnt=((IGXDLMSBase)it.getValue()).getAttributeCount();
        if (sn >= it.getKey() && sn <= (it.getKey() + (8 * aCnt))) {
          item=it.getValue();
          attributeIndex=((sn - item.getShortName()) / 8) + 1;
          AccessMode acc=item.getAccess(attributeIndex);
          if (acc == AccessMode.NO_ACCESS || acc == AccessMode.READ || acc == AccessMode.AUTHENTICATED_READ) {
            SendData.addAll(Arrays.asList(serverReportError(Command.forValue(command[0]),3)));
            return SendData.get(FrameIndex);
          }
          if (value instanceof Byte[]) {
            DataType tp=item.getUIDataType(attributeIndex);
            if (tp != DataType.NONE) {
              value[0]=GXDLMSClient.changeType((byte[])value[0],tp);
            }
          }
          ValueEventArgs e=new ValueEventArgs(item,attributeIndex,selector[0]);
          e.setValue(value[0]);
          write(e);
          if (!e.getHandled()) {
            ((IGXDLMSBase)item).setValue(attributeIndex,value[0]);
          }
          SendData.addAll(Arrays.asList(acknowledge(getUseLogicalNameReferencing() ? Command.SetResponse : Command.WriteResponse,0)));
          return SendData.get(FrameIndex);
        }
      }
      if (item == null) {
        throw new IllegalArgumentException("String_Node_Str");
      }
    }
 else     if (command[0] == Command.SetRequest.getValue()) {
      int attributeIndex[]=new int[1];
      Object[] value=new Object[1];
      ObjectType[] type=new ObjectType[1];
      int[] selector=new int[1];
      getCommand(command[0],allData,type,names,attributeIndex,selector,value);
      item=getItems().findByLN(type[0],names.get(0).toString());
      if (item != null) {
        AccessMode acc=item.getAccess(attributeIndex[0]);
        if (acc == AccessMode.NO_ACCESS || acc == AccessMode.READ || acc == AccessMode.AUTHENTICATED_READ) {
          SendData.addAll(Arrays.asList(serverReportError(Command.forValue(command[0]),3)));
          return SendData.get(FrameIndex);
        }
        if (value instanceof Byte[]) {
          DataType tp=item.getUIDataType(attributeIndex[0]);
          if (tp != DataType.NONE) {
            value[0]=GXDLMSClient.changeType((byte[])value[0],tp);
          }
        }
        ValueEventArgs e=new ValueEventArgs(item,attributeIndex[0],selector[0]);
        e.setValue(value[0]);
        write(e);
        if (!e.getHandled()) {
          ((IGXDLMSBase)item).setValue(attributeIndex[0],value[0]);
        }
        SendData.addAll(Arrays.asList(acknowledge(getUseLogicalNameReferencing() ? Command.SetResponse : Command.WriteResponse,0)));
        return SendData.get(FrameIndex);
      }
      throw new IllegalArgumentException("String_Node_Str");
    }
 else     if (command[0] == Command.ReadRequest.getValue() && !getUseLogicalNameReferencing()) {
      int attributeIndex;
      int tmp2[]=new int[1];
      Object[] value=new Object[1];
      ObjectType[] type=new ObjectType[1];
      int[] selector=new int[1];
      getCommand(command[0],allData,type,names,tmp2,selector,value);
      int sn=(int)names.get(0);
      for (      Map.Entry<Integer,GXDLMSObject> it : SortedItems.entrySet()) {
        int aCnt=((IGXDLMSBase)it.getValue()).getAttributeCount();
        if (sn >= it.getKey() && sn <= (it.getKey() + (8 * aCnt))) {
          item=it.getValue();
          attributeIndex=((sn - item.getShortName()) / 8) + 1;
          ValueEventArgs e=new ValueEventArgs(item,attributeIndex,selector[0]);
          e.setValue(value[0]);
          read(e);
          if (e.getHandled()) {
            DataType tp=item.getDataType(attributeIndex);
            SendData.addAll(Arrays.asList(readReply(names.get(0),type[0],attributeIndex,e.getValue(),tp)));
            return SendData.get(FrameIndex);
          }
          if (item != null) {
            return getValue(names.get(0),item,attributeIndex,selector[0],value[0]);
          }
        }
 else         if (sn >= it.getKey() + aCnt && ((IGXDLMSBase)it.getValue()).getMethodCount() != 0) {
          int[] value2=new int[1], count=new int[1];
          GXDLMS.getActionInfo(it.getValue().getObjectType(),value2,count);
          if (sn <= it.getKey() + value2[0] + (8 * count[0])) {
            item=it.getValue();
            attributeIndex=((sn - item.getShortName() - value2[0]) / 8) + 1;
            ValueEventArgs e=new ValueEventArgs(item,attributeIndex,selector[0]);
            e.setValue(value[0]);
            action(e);
            if (!e.getHandled()) {
              byte[][] reply=((IGXDLMSBase)item).invoke(this,attributeIndex,e.getValue());
              if (reply != null) {
                SendData.addAll(Arrays.asList(reply));
                return SendData.get(FrameIndex);
              }
            }
            SendData.addAll(Arrays.asList(acknowledge(Command.MethodResponse,0)));
            return SendData.get(FrameIndex);
          }
        }
      }
      throw new IllegalArgumentException("String_Node_Str");
    }
 else     if (command[0] == Command.GetRequest.getValue() && getUseLogicalNameReferencing()) {
      ObjectType[] type=new ObjectType[]{ObjectType.NONE};
      int[] index=new int[1];
      Object[] parameter=new Object[1];
      int[] selector=new int[1];
      getCommand(command[0],allData,type,names,index,selector,parameter);
      System.out.println(String.format("String_Node_Str",names,index[0]));
      item=m_Items.findByLN(type[0],names.get(0).toString());
      if (item != null) {
        ValueEventArgs e=new ValueEventArgs(item,index[0],selector[0]);
        read(e);
        if (e.getHandled()) {
          SendData.addAll(Arrays.asList(readReply(names.get(0),type[0],index[0],e.getValue(),e.getDataType())));
          return SendData.get(FrameIndex);
        }
        return getValue(names.get(0),item,index[0],selector[0],parameter[0]);
      }
    }
 else     if (command[0] == Command.MethodRequest.getValue()) {
      ObjectType[] type=new ObjectType[1];
      int[] index=new int[1];
      Object[] parameter=new Object[1];
      int[] selector=new int[1];
      getCommand(command[0],allData,type,names,index,selector,parameter);
      item=m_Items.findByLN(type[0],names.get(0).toString());
      if (item != null) {
        System.out.println(String.format("String_Node_Str",names.get(0),index[0]));
        ValueEventArgs e=new ValueEventArgs(item,index[0],selector[0]);
        e.setValue(parameter[0]);
        action(e);
        if (!e.getHandled()) {
          byte[][] reply=item.invoke(this,index[0],e.getValue());
          if (reply != null) {
            SendData.addAll(Arrays.asList(reply));
            return SendData.get(FrameIndex);
          }
        }
        SendData.addAll(Arrays.asList(acknowledge(Command.MethodResponse,0)));
        return SendData.get(FrameIndex);
      }
    }
    SendData.addAll(Arrays.asList(serverReportError(Command.forValue(command[0]),3)));
    return SendData.get(FrameIndex);
  }
 catch (  java.lang.Exception e) {
    ReceivedFrame.reset();
    SendData.addAll(Arrays.asList(serverReportError(Command.forValue(command[0]),3)));
    return SendData.get(FrameIndex);
  }
}

Fixed version (ground truth):
/** 
 * Mandles client request.
 * @param buff Received data from the client.
 * @return Response to the request.        Response is null if request packet is not compleate.
 */
public final byte[] handleRequest(byte[] buff){
  if (buff == null) {
    return null;
  }
  if (!Initialized) {
    throw new RuntimeException("String_Node_Str");
  }
  int[] command=new int[1];
  try {
    byte[] data;
    if (ReceivedFrame.size() != 0) {
      ReceivedFrame.write(buff);
      data=ReceivedFrame.toByteArray();
    }
 else {
      data=buff;
    }
    if (m_Base.getServerID() == null) {
      Object[] sid=new Object[1], cid=new Object[1];
      getAddress(data,cid,sid);
      for (      Object it : this.getServerIDs()) {
        if (((Number)sid[0]).intValue() == ((Number)it).intValue()) {
          m_Base.setServerID(sid[0]);
          m_Base.setClientID(cid[0]);
          break;
        }
      }
      if (m_Base.getServerID() == null) {
        invalidConnection(new ConnectionEventArgs(sid));
        return null;
      }
    }
    if (!m_Base.isDLMSPacketComplete(data)) {
      if (ReceivedFrame.size() == 0) {
        ReceivedFrame.write(buff);
      }
      return null;
    }
    List<Object> names=new ArrayList<Object>();
    GXDLMSObject item=null;
    ByteArrayOutputStream tmp=new ByteArrayOutputStream();
    byte[] frame=new byte[1];
    java.util.Set<RequestTypes> ret=m_Base.getDataFromPacket(data,tmp,frame,command);
    byte[] allData=tmp.toByteArray();
    ReceivedFrame.reset();
    if (ret.contains(RequestTypes.FRAME)) {
      ++FrameIndex;
      if ((frame[0] & 0x1) == 0) {
        if (command[0] != 0) {
          LastCommand=command[0];
        }
        ReceivedData.write(allData);
        SendData.clear();
        FrameIndex=0;
        --m_Base.expectedFrame;
        return m_Base.receiverReady(RequestTypes.FRAME);
      }
 else       if (FrameIndex >= SendData.size() && (frame[0] & 0x1) == 1) {
        SendData.clear();
        FrameIndex=0;
        return m_Base.addFrame(m_Base.generateAliveFrame(),false,(byte[])null,0,0);
      }
      return SendData.get(FrameIndex);
    }
 else     if (ret.contains(RequestTypes.DATABLOCK)) {
      if ((frame[0] & 0x1) == 0) {
        if (command[0] != 0) {
          LastCommand=command[0];
        }
        ReceivedData.write(allData);
        SendData.clear();
        FrameIndex=0;
        return m_Base.receiverReady(RequestTypes.DATABLOCK);
      }
      ++FrameIndex;
      int[] index=new int[1];
      int BlockIndex=(int)GXCommon.getUInt32(allData,index);
      return SendData.get(FrameIndex);
    }
    if (ReceivedData.size() != 0) {
      ReceivedData.write(allData);
      allData=ReceivedData.toByteArray();
      ReceivedData.reset();
      command[0]=LastCommand;
    }
    FrameIndex=0;
    SendData.clear();
    if (command[0] == Command.GloGetRequest.getValue() || command[0] == Command.GloSetRequest.getValue() || command[0] == Command.GloMethodRequest.getValue()) {
      Command[] cmd=new Command[1];
      allData=m_Base.decrypt(allData,cmd);
      command[0]=cmd[0].getValue();
    }
    if (command[0] == Command.Snrm.getValue()) {
      SendData.add(handleSnrmRequest());
      return SendData.get(FrameIndex);
    }
 else     if (command[0] == Command.Aarq.getValue()) {
      SendData.add(handleAARQRequest(data));
      return SendData.get(FrameIndex);
    }
 else     if (command[0] == Command.DisconnectRequest.getValue()) {
      System.out.println("String_Node_Str");
      SendData.add(generateDisconnectRequest());
      return SendData.get(FrameIndex);
    }
 else     if (command[0] == Command.WriteRequest.getValue()) {
      int attributeIndex;
      int tmp2[]=new int[1];
      Object[] value=new Object[1];
      ObjectType[] type=new ObjectType[1];
      int[] selector=new int[1];
      getCommand(command[0],allData,type,names,tmp2,selector,value);
      int sn=(int)names.get(0);
      for (      Map.Entry<Integer,GXDLMSObject> it : SortedItems.entrySet()) {
        int aCnt=((IGXDLMSBase)it.getValue()).getAttributeCount();
        if (sn >= it.getKey() && sn <= (it.getKey() + (8 * aCnt))) {
          item=it.getValue();
          attributeIndex=((sn - item.getShortName()) / 8) + 1;
          AccessMode acc=item.getAccess(attributeIndex);
          if (acc == AccessMode.NO_ACCESS || acc == AccessMode.READ || acc == AccessMode.AUTHENTICATED_READ) {
            SendData.addAll(Arrays.asList(serverReportError(Command.forValue(command[0]),3)));
            return SendData.get(FrameIndex);
          }
          if (value instanceof Byte[]) {
            DataType tp=item.getUIDataType(attributeIndex);
            if (tp != DataType.NONE) {
              value[0]=GXDLMSClient.changeType((byte[])value[0],tp);
            }
          }
          ValueEventArgs e=new ValueEventArgs(item,attributeIndex,selector[0]);
          e.setValue(value[0]);
          write(e);
          if (!e.getHandled()) {
            ((IGXDLMSBase)item).setValue(attributeIndex,value[0]);
          }
          SendData.addAll(Arrays.asList(acknowledge(getUseLogicalNameReferencing() ? Command.SetResponse : Command.WriteResponse,0)));
          return SendData.get(FrameIndex);
        }
      }
      if (item == null) {
        throw new IllegalArgumentException("String_Node_Str");
      }
    }
 else     if (command[0] == Command.SetRequest.getValue()) {
      int attributeIndex[]=new int[1];
      Object[] value=new Object[1];
      ObjectType[] type=new ObjectType[1];
      int[] selector=new int[1];
      getCommand(command[0],allData,type,names,attributeIndex,selector,value);
      item=getItems().findByLN(type[0],names.get(0).toString());
      if (item != null) {
        AccessMode acc=item.getAccess(attributeIndex[0]);
        if (acc == AccessMode.NO_ACCESS || acc == AccessMode.READ || acc == AccessMode.AUTHENTICATED_READ) {
          SendData.addAll(Arrays.asList(serverReportError(Command.forValue(command[0]),3)));
          return SendData.get(FrameIndex);
        }
        if (value instanceof Byte[]) {
          DataType tp=item.getUIDataType(attributeIndex[0]);
          if (tp != DataType.NONE) {
            value[0]=GXDLMSClient.changeType((byte[])value[0],tp);
          }
        }
        ValueEventArgs e=new ValueEventArgs(item,attributeIndex[0],selector[0]);
        e.setValue(value[0]);
        write(e);
        if (!e.getHandled()) {
          ((IGXDLMSBase)item).setValue(attributeIndex[0],value[0]);
        }
        SendData.addAll(Arrays.asList(acknowledge(getUseLogicalNameReferencing() ? Command.SetResponse : Command.WriteResponse,0)));
        return SendData.get(FrameIndex);
      }
      throw new IllegalArgumentException("String_Node_Str");
    }
 else     if (command[0] == Command.ReadRequest.getValue() && !getUseLogicalNameReferencing()) {
      int attributeIndex;
      int tmp2[]=new int[1];
      Object[] value=new Object[1];
      ObjectType[] type=new ObjectType[1];
      int[] selector=new int[1];
      getCommand(command[0],allData,type,names,tmp2,selector,value);
      int sn=(int)names.get(0);
      for (      Map.Entry<Integer,GXDLMSObject> it : SortedItems.entrySet()) {
        int aCnt=((IGXDLMSBase)it.getValue()).getAttributeCount();
        if (sn >= it.getKey() && sn <= (it.getKey() + (8 * aCnt))) {
          item=it.getValue();
          attributeIndex=((sn - item.getShortName()) / 8) + 1;
          System.out.println(String.format("String_Node_Str",item.getName(),attributeIndex));
          ValueEventArgs e=new ValueEventArgs(item,attributeIndex,selector[0]);
          e.setValue(value[0]);
          read(e);
          if (e.getHandled()) {
            DataType tp=item.getDataType(attributeIndex);
            SendData.addAll(Arrays.asList(readReply(names.get(0),type[0],attributeIndex,e.getValue(),tp)));
            return SendData.get(FrameIndex);
          }
          if (item != null) {
            return getValue(names.get(0),item,attributeIndex,selector[0],value[0]);
          }
        }
 else         if (sn >= it.getKey() + aCnt && ((IGXDLMSBase)it.getValue()).getMethodCount() != 0) {
          int[] value2=new int[1], count=new int[1];
          GXDLMS.getActionInfo(it.getValue().getObjectType(),value2,count);
          if (sn <= it.getKey() + value2[0] + (8 * count[0])) {
            item=it.getValue();
            attributeIndex=((sn - item.getShortName() - value2[0]) / 8) + 1;
            System.out.println(String.format("String_Node_Str",item.getName(),attributeIndex));
            ValueEventArgs e=new ValueEventArgs(item,attributeIndex,selector[0]);
            e.setValue(value[0]);
            action(e);
            if (!e.getHandled()) {
              byte[][] reply=((IGXDLMSBase)item).invoke(this,attributeIndex,e.getValue());
              if (reply != null) {
                SendData.addAll(Arrays.asList(reply));
                return SendData.get(FrameIndex);
              }
            }
            SendData.addAll(Arrays.asList(acknowledge(Command.MethodResponse,0)));
            return SendData.get(FrameIndex);
          }
        }
      }
      throw new IllegalArgumentException("String_Node_Str");
    }
 else     if (command[0] == Command.GetRequest.getValue() && getUseLogicalNameReferencing()) {
      ObjectType[] type=new ObjectType[]{ObjectType.NONE};
      int[] index=new int[1];
      Object[] parameter=new Object[1];
      int[] selector=new int[1];
      getCommand(command[0],allData,type,names,index,selector,parameter);
      System.out.println(String.format("String_Node_Str",names,index[0]));
      item=m_Items.findByLN(type[0],names.get(0).toString());
      if (item != null) {
        ValueEventArgs e=new ValueEventArgs(item,index[0],selector[0]);
        read(e);
        if (e.getHandled()) {
          SendData.addAll(Arrays.asList(readReply(names.get(0),type[0],index[0],e.getValue(),e.getDataType())));
          return SendData.get(FrameIndex);
        }
        return getValue(names.get(0),item,index[0],selector[0],parameter[0]);
      }
    }
 else     if (command[0] == Command.MethodRequest.getValue()) {
      ObjectType[] type=new ObjectType[1];
      int[] index=new int[1];
      Object[] parameter=new Object[1];
      int[] selector=new int[1];
      getCommand(command[0],allData,type,names,index,selector,parameter);
      item=m_Items.findByLN(type[0],names.get(0).toString());
      if (item != null) {
        System.out.println(String.format("String_Node_Str",names.get(0),index[0]));
        ValueEventArgs e=new ValueEventArgs(item,index[0],selector[0]);
        e.setValue(parameter[0]);
        action(e);
        if (!e.getHandled()) {
          byte[][] reply=item.invoke(this,index[0],e.getValue());
          if (reply != null) {
            SendData.addAll(Arrays.asList(reply));
            return SendData.get(FrameIndex);
          }
        }
        SendData.addAll(Arrays.asList(acknowledge(Command.MethodResponse,0)));
        return SendData.get(FrameIndex);
      }
    }
    SendData.addAll(Arrays.asList(serverReportError(Command.forValue(command[0]),3)));
    return SendData.get(FrameIndex);
  }
 catch (  java.lang.Exception e) {
    ReceivedFrame.reset();
    SendData.addAll(Arrays.asList(serverReportError(Command.forValue(command[0]),3)));
    return SendData.get(FrameIndex);
  }
}