Program: 156046
Project: 156046
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,remoteAddress,id,MSG_LOGIN_RESPONSE,response);
    }
  return null;
case MSG_HEARTBEAT:
if (channel != null) {
  response=ChannelBuffers.wrappedBuffer(new byte[]{0x01});
  sendResponse(channel,remoteAddress,id,MSG_HEARTBEAT,response);
}
return null;
case MSG_SERVER:
if (channel != null) {
response=ChannelBuffers.copiedBuffer(getMeiligaoServer(channel),StandardCharsets.US_ASCII);
sendResponse(channel,remoteAddress,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(Position.KEY_ALARM,buf.readUnsignedByte());
}
 else if (command == MSG_POSITION_LOGGED) {
buf.skipBytes(6);
}
if (!identify(id,channel,remoteAddress)) {
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(Position.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,StandardCharsets.US_ASCII));
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.setValid(true);
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(Position.KEY_HDOP,parser.next());
if (parser.hasNext()) {
position.setAltitude(parser.nextDouble());
}
position.set(Position.KEY_STATUS,parser.next());
for (int i=1; i <= 8; i++) {
if (parser.hasNext()) {
position.set(Position.PREFIX_ADC + i,parser.nextInt(16));
}
}
if (parser.hasNext()) {
position.set(Position.KEY_GSM,parser.nextInt(16));
}
if (parser.hasNext()) {
position.set(Position.KEY_ODOMETER,parser.nextInt(16));
}
if (parser.hasNext()) {
position.set(Position.KEY_ODOMETER,parser.nextInt(16));
}
if (parser.hasNext()) {
position.set(Position.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,remoteAddress,id,MSG_LOGIN_RESPONSE,response);
    }
  return null;
case MSG_HEARTBEAT:
if (channel != null) {
  response=ChannelBuffers.wrappedBuffer(new byte[]{0x01});
  sendResponse(channel,remoteAddress,id,MSG_HEARTBEAT,response);
}
return null;
case MSG_SERVER:
if (channel != null) {
response=ChannelBuffers.copiedBuffer(getMeiligaoServer(channel),StandardCharsets.US_ASCII);
sendResponse(channel,remoteAddress,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(Position.KEY_ALARM,buf.readUnsignedByte());
}
 else if (command == MSG_POSITION_LOGGED) {
buf.skipBytes(6);
}
if (!identify(id,channel,remoteAddress)) {
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(Position.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,StandardCharsets.US_ASCII));
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.setValid(true);
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(Position.KEY_HDOP,parser.next());
if (parser.hasNext()) {
position.setAltitude(parser.nextDouble());
}
position.set(Position.KEY_STATUS,parser.next());
for (int i=1; i <= 8; i++) {
if (parser.hasNext()) {
position.set(Position.PREFIX_ADC + i,parser.nextInt(16));
}
}
if (parser.hasNext()) {
position.set(Position.KEY_GSM,parser.nextInt(16));
}
if (parser.hasNext()) {
position.set(Position.KEY_ODOMETER,parser.nextLong(16));
}
if (parser.hasNext()) {
position.set(Position.KEY_ODOMETER,parser.nextLong(16));
}
if (parser.hasNext()) {
position.set(Position.KEY_RFID,parser.nextInt(16));
}
}
return position;
}