Program: 156112
Project: 156112
Buggy source:
@Override protected Object decode(Channel channel,SocketAddress remoteAddress,Object msg) throws Exception {
  ChannelBuffer buf=(ChannelBuffer)msg;
  buf.skipBytes(2);
  buf.readShort();
  ChannelBuffer id=buf.readBytes(7);
  int command=buf.readUnsignedShort();
  ChannelBuffer response;
switch (command) {
case MSG_LOGIN:
    if (channel != null) {
      response=ChannelBuffers.wrappedBuffer(new byte[]{0x01});
      sendResponse(channel,id,MSG_LOGIN_RESPONSE,response);
    }
  return null;
case MSG_HEARTBEAT:
if (channel != null) {
  response=ChannelBuffers.wrappedBuffer(new byte[]{0x01});
  sendResponse(channel,id,MSG_HEARTBEAT,response);
}
return null;
case MSG_SERVER:
if (channel != null) {
response=ChannelBuffers.copiedBuffer(getMeiligaoServer(channel),Charset.defaultCharset());
sendResponse(channel,id,MSG_SERVER,response);
}
return null;
case MSG_POSITION:
case MSG_POSITION_LOGGED:
case MSG_ALARM:
case MSG_RFID:
break;
default :
return null;
}
Position position=new Position();
position.setProtocol(getProtocolName());
if (command == MSG_ALARM) {
position.set(Event.KEY_ALARM,buf.readUnsignedByte());
}
 else if (command == MSG_POSITION_LOGGED) {
buf.skipBytes(6);
}
if (!identify(id,channel)) {
return null;
}
position.setDeviceId(getDeviceId());
if (command == MSG_RFID) {
for (int i=0; i < 15; i++) {
long rfid=buf.readUnsignedInt();
if (rfid != 0) {
String card=String.format("String_Node_Str",rfid);
position.set("String_Node_Str" + (i + 1),card);
position.set(Event.KEY_RFID,card);
}
}
}
Parser parser=new Parser(command == MSG_RFID ? PATTERN_RFID : PATTERN,buf.toString(buf.readerIndex(),buf.readableBytes() - 4,Charset.defaultCharset()));
if (!parser.matches()) {
return null;
}
if (command == MSG_RFID) {
DateBuilder dateBuilder=new DateBuilder().setTime(parser.nextInt(),parser.nextInt(),parser.nextInt()).setDateReverse(parser.nextInt(),parser.nextInt(),parser.nextInt());
position.setTime(dateBuilder.getDate());
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
}
 else {
DateBuilder dateBuilder=new DateBuilder().setTime(parser.nextInt(),parser.nextInt(),parser.nextInt());
if (parser.hasNext()) {
dateBuilder.setMillis(parser.nextInt());
}
position.setValid(parser.next().equals("String_Node_Str"));
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
if (parser.hasNext()) {
position.setSpeed(parser.nextDouble());
}
if (parser.hasNext()) {
position.setCourse(parser.nextDouble());
}
dateBuilder.setDateReverse(parser.nextInt(),parser.nextInt(),parser.nextInt());
position.setTime(dateBuilder.getDate());
position.set(Event.KEY_HDOP,parser.next());
if (parser.hasNext()) {
position.setAltitude(parser.nextDouble());
}
position.set(Event.KEY_STATUS,parser.next());
for (int i=1; i <= 8; i++) {
if (parser.hasNext()) {
position.set(Event.PREFIX_ADC + i,parser.nextInt(16));
}
}
position.set(Event.KEY_CELL,parser.next());
if (parser.hasNext()) {
position.set(Event.KEY_GSM,parser.nextInt(16));
}
if (parser.hasNext()) {
position.set(Event.KEY_ODOMETER,parser.nextInt(16));
}
 else if (parser.hasNext()) {
position.set(Event.KEY_ODOMETER,parser.nextInt(16));
}
if (parser.hasNext()) {
position.set(Event.KEY_RFID,parser.nextInt(16));
}
}
return position;
}

Fixed version (ground truth):
@Override protected Object decode(Channel channel,SocketAddress remoteAddress,Object msg) throws Exception {
  ChannelBuffer buf=(ChannelBuffer)msg;
  buf.skipBytes(2);
  buf.readShort();
  ChannelBuffer id=buf.readBytes(7);
  int command=buf.readUnsignedShort();
  ChannelBuffer response;
switch (command) {
case MSG_LOGIN:
    if (channel != null) {
      response=ChannelBuffers.wrappedBuffer(new byte[]{0x01});
      sendResponse(channel,id,MSG_LOGIN_RESPONSE,response);
    }
  return null;
case MSG_HEARTBEAT:
if (channel != null) {
  response=ChannelBuffers.wrappedBuffer(new byte[]{0x01});
  sendResponse(channel,id,MSG_HEARTBEAT,response);
}
return null;
case MSG_SERVER:
if (channel != null) {
response=ChannelBuffers.copiedBuffer(getMeiligaoServer(channel),Charset.defaultCharset());
sendResponse(channel,id,MSG_SERVER,response);
}
return null;
case MSG_POSITION:
case MSG_POSITION_LOGGED:
case MSG_ALARM:
case MSG_RFID:
break;
default :
return null;
}
Position position=new Position();
position.setProtocol(getProtocolName());
if (command == MSG_ALARM) {
position.set(Event.KEY_ALARM,buf.readUnsignedByte());
}
 else if (command == MSG_POSITION_LOGGED) {
buf.skipBytes(6);
}
if (!identify(id,channel)) {
return null;
}
position.setDeviceId(getDeviceId());
if (command == MSG_RFID) {
for (int i=0; i < 15; i++) {
long rfid=buf.readUnsignedInt();
if (rfid != 0) {
String card=String.format("String_Node_Str",rfid);
position.set("String_Node_Str" + (i + 1),card);
position.set(Event.KEY_RFID,card);
}
}
}
Pattern pattern;
if (command == MSG_RFID) {
pattern=PATTERN_RFID;
}
 else {
pattern=PATTERN;
}
Parser parser=new Parser(pattern,buf.toString(buf.readerIndex(),buf.readableBytes() - 4,Charset.defaultCharset()));
if (!parser.matches()) {
return null;
}
if (command == MSG_RFID) {
DateBuilder dateBuilder=new DateBuilder().setTime(parser.nextInt(),parser.nextInt(),parser.nextInt()).setDateReverse(parser.nextInt(),parser.nextInt(),parser.nextInt());
position.setTime(dateBuilder.getDate());
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
}
 else {
DateBuilder dateBuilder=new DateBuilder().setTime(parser.nextInt(),parser.nextInt(),parser.nextInt());
if (parser.hasNext()) {
dateBuilder.setMillis(parser.nextInt());
}
position.setValid(parser.next().equals("String_Node_Str"));
position.setLatitude(parser.nextCoordinate());
position.setLongitude(parser.nextCoordinate());
if (parser.hasNext()) {
position.setSpeed(parser.nextDouble());
}
if (parser.hasNext()) {
position.setCourse(parser.nextDouble());
}
dateBuilder.setDateReverse(parser.nextInt(),parser.nextInt(),parser.nextInt());
position.setTime(dateBuilder.getDate());
position.set(Event.KEY_HDOP,parser.next());
if (parser.hasNext()) {
position.setAltitude(parser.nextDouble());
}
position.set(Event.KEY_STATUS,parser.next());
for (int i=1; i <= 8; i++) {
if (parser.hasNext()) {
position.set(Event.PREFIX_ADC + i,parser.nextInt(16));
}
}
position.set(Event.KEY_CELL,parser.next());
if (parser.hasNext()) {
position.set(Event.KEY_GSM,parser.nextInt(16));
}
if (parser.hasNext()) {
position.set(Event.KEY_ODOMETER,parser.nextInt(16));
}
 else if (parser.hasNext()) {
position.set(Event.KEY_ODOMETER,parser.nextInt(16));
}
if (parser.hasNext()) {
position.set(Event.KEY_RFID,parser.nextInt(16));
}
}
return position;
}