Program: 156198
Project: 156198
Buggy source:
@Override protected Object decode(ChannelHandlerContext ctx,Channel channel,SocketAddress remoteAddress,Object msg) throws Exception {
  ChannelBuffer buf=(ChannelBuffer)msg;
  buf.skipBytes(2);
  buf.readUnsignedShort();
  int version=buf.readUnsignedByte();
  ChannelBuffer id=buf.readBytes(20);
  int type=ChannelBuffers.swapShort(buf.readShort());
  if (type == MSG_HEARTBEAT) {
    if (channel != null) {
      ChannelBuffer response=ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN,31);
      response.writeByte(0x40);
      response.writeByte(0x40);
      response.writeShort(response.capacity());
      response.writeByte(version);
      response.writeBytes(id);
      response.writeShort(ChannelBuffers.swapShort(MSG_HEARTBEAT_RESPONSE));
      response.writeShort(Crc.crc16Ccitt(response.toByteBuffer(0,response.writerIndex())));
      response.writeByte(0x0D);
      response.writeByte(0x0A);
      channel.write(response,remoteAddress);
    }
  }
 else   if (type == MSG_LOGIN || type == MSG_GPS) {
    Position position=new Position();
    position.setProtocol(getProtocolName());
    if (!identify(id.toString(Charset.defaultCharset()).trim(),channel,remoteAddress)) {
      return null;
    }
 else     if (type == MSG_LOGIN) {
      if (channel != null) {
        ChannelBuffer response=ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN,41);
        response.writeByte(0x40);
        response.writeByte(0x40);
        response.writeShort(response.capacity());
        response.writeByte(version);
        response.writeBytes(id);
        response.writeShort(ChannelBuffers.swapShort(MSG_LOGIN_RESPONSE));
        response.writeInt(0xFFFFFFFF);
        response.writeShort(0);
        response.writeInt((int)(new Date().getTime() / 1000));
        response.writeShort(Crc.crc16Ccitt(response.toByteBuffer(0,response.writerIndex())));
        response.writeByte(0x0D);
        response.writeByte(0x0A);
        channel.write(response,remoteAddress);
      }
    }
    position.setDeviceId(getDeviceId());
    if (type == MSG_GPS) {
      buf.readUnsignedByte();
    }
    buf.readUnsignedInt();
    buf.readUnsignedInt();
    position.set(Event.KEY_ODOMETER,buf.readUnsignedInt());
    buf.readUnsignedInt();
    buf.readUnsignedInt();
    buf.readUnsignedShort();
    position.set(Event.KEY_STATUS,buf.readUnsignedInt());
    buf.skipBytes(8);
    buf.readUnsignedByte();
    Calendar time=Calendar.getInstance(TimeZone.getTimeZone("String_Node_Str"));
    time.clear();
    time.set(Calendar.DAY_OF_MONTH,buf.readUnsignedByte());
    time.set(Calendar.MONTH,buf.readUnsignedByte() - 1);
    time.set(Calendar.YEAR,2000 + buf.readUnsignedByte());
    time.set(Calendar.HOUR_OF_DAY,buf.readUnsignedByte());
    time.set(Calendar.MINUTE,buf.readUnsignedByte());
    time.set(Calendar.SECOND,buf.readUnsignedByte());
    position.setTime(time.getTime());
    double lat=buf.readUnsignedInt() / 3600000.0;
    double lon=buf.readUnsignedInt() / 3600000.0;
    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
    position.setCourse(buf.readUnsignedShort() % 360);
    int flags=buf.readUnsignedByte();
    position.setLatitude((flags & 0x01) == 0 ? -lat : lat);
    position.setLongitude((flags & 0x02) == 0 ? -lon : lon);
    position.setValid((flags & 0x0C) > 0);
    position.set(Event.KEY_SATELLITES,flags >> 4);
    return position;
  }
  return null;
}

Fixed version (ground truth):
@Override protected Object decode(ChannelHandlerContext ctx,Channel channel,SocketAddress remoteAddress,Object msg) throws Exception {
  ChannelBuffer buf=(ChannelBuffer)msg;
  buf.skipBytes(2);
  buf.readUnsignedShort();
  int version=buf.readUnsignedByte();
  ChannelBuffer id=buf.readBytes(20);
  int type=ChannelBuffers.swapShort(buf.readShort());
  if (type == MSG_HEARTBEAT) {
    if (channel != null) {
      ChannelBuffer response=ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN,31);
      response.writeByte(0x40);
      response.writeByte(0x40);
      response.writeShort(response.capacity());
      response.writeByte(version);
      response.writeBytes(id);
      response.writeShort(ChannelBuffers.swapShort(MSG_HEARTBEAT_RESPONSE));
      response.writeShort(Crc.crc16Ccitt(response.toByteBuffer(0,response.writerIndex())));
      response.writeByte(0x0D);
      response.writeByte(0x0A);
      channel.write(response,remoteAddress);
    }
  }
 else   if (type == MSG_LOGIN || type == MSG_GPS) {
    Position position=new Position();
    position.setProtocol(getProtocolName());
    if (!identify(id.toString(Charset.defaultCharset()).trim(),channel,remoteAddress)) {
      return null;
    }
 else     if (type == MSG_LOGIN) {
      if (channel != null) {
        ChannelBuffer response=ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN,41);
        response.writeByte(0x40);
        response.writeByte(0x40);
        response.writeShort(response.capacity());
        response.writeByte(version);
        response.writeBytes(id);
        response.writeShort(ChannelBuffers.swapShort(MSG_LOGIN_RESPONSE));
        response.writeInt(0xFFFFFFFF);
        response.writeShort(0);
        response.writeInt((int)(new Date().getTime() / 1000));
        response.writeShort(Crc.crc16Ccitt(response.toByteBuffer(0,response.writerIndex())));
        response.writeByte(0x0D);
        response.writeByte(0x0A);
        channel.write(response,remoteAddress);
      }
    }
    position.setDeviceId(getDeviceId());
    if (type == MSG_GPS) {
      buf.readUnsignedByte();
    }
    buf.readUnsignedInt();
    buf.readUnsignedInt();
    position.set(Event.KEY_ODOMETER,buf.readUnsignedInt());
    buf.readUnsignedInt();
    buf.readUnsignedInt();
    buf.readUnsignedShort();
    position.set(Event.KEY_STATUS,buf.readUnsignedInt());
    buf.skipBytes(8);
    buf.readUnsignedByte();
    Calendar time=Calendar.getInstance(TimeZone.getTimeZone("String_Node_Str"));
    time.clear();
    time.set(Calendar.DAY_OF_MONTH,buf.readUnsignedByte());
    time.set(Calendar.MONTH,buf.readUnsignedByte() - 1);
    time.set(Calendar.YEAR,2000 + buf.readUnsignedByte());
    time.set(Calendar.HOUR_OF_DAY,buf.readUnsignedByte());
    time.set(Calendar.MINUTE,buf.readUnsignedByte());
    time.set(Calendar.SECOND,buf.readUnsignedByte());
    position.setTime(time.getTime());
    double lat=buf.readUnsignedInt() / 3600000.0;
    double lon=buf.readUnsignedInt() / 3600000.0;
    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
    position.setCourse(buf.readUnsignedShort() % 360);
    int flags=buf.readUnsignedByte();
    position.setLatitude((flags & 0x02) == 0 ? -lat : lat);
    position.setLongitude((flags & 0x01) == 0 ? -lon : lon);
    position.setValid((flags & 0x0C) > 0);
    position.set(Event.KEY_SATELLITES,flags >> 4);
    return position;
  }
  return null;
}