Program: 155951
Project: 155951
Buggy source:
@Override protected Object decode(Channel channel,SocketAddress remoteAddress,Object msg) throws Exception {
  ChannelBuffer buf=(ChannelBuffer)msg;
  buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3));
  List<Position> positions=new LinkedList<>();
  while (buf.readableBytes() > 2) {
    int length=buf.readUnsignedShort();
    int index=buf.readUnsignedShort();
    int recordFlags=buf.readUnsignedByte();
    if (BitUtil.check(recordFlags,0)) {
      buf.readUnsignedInt();
    }
    if (BitUtil.check(recordFlags,1)) {
      buf.readUnsignedInt();
    }
    if (BitUtil.check(recordFlags,2)) {
      buf.readUnsignedInt();
    }
    int serviceType=buf.readUnsignedByte();
    buf.readUnsignedByte();
    int recordEnd=buf.readerIndex() + length;
    Position position=new Position(getProtocolName());
    DeviceSession deviceSession=getDeviceSession(channel,remoteAddress);
    if (deviceSession != null) {
      position.setDeviceId(deviceSession.getDeviceId());
    }
    ChannelBuffer response=ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN,0);
    response.writeShort(index);
    response.writeByte(0);
    sendResponse(channel,PT_RESPONSE,index,serviceType,MSG_RECORD_RESPONSE,response);
    while (buf.readerIndex() < recordEnd) {
      int type=buf.readUnsignedByte();
      int end=buf.readUnsignedShort() + buf.readerIndex();
      if (type == MSG_TERM_IDENTITY) {
        buf.readUnsignedInt();
        int flags=buf.readUnsignedByte();
        if (BitUtil.check(flags,0)) {
          buf.readUnsignedShort();
        }
        if (BitUtil.check(flags,1)) {
          getDeviceSession(channel,remoteAddress,buf.readBytes(15).toString(StandardCharsets.US_ASCII));
        }
        if (BitUtil.check(flags,2)) {
          getDeviceSession(channel,remoteAddress,buf.readBytes(16).toString(StandardCharsets.US_ASCII));
        }
        if (BitUtil.check(flags,3)) {
          buf.skipBytes(3);
        }
        if (BitUtil.check(flags,5)) {
          buf.skipBytes(3);
        }
        if (BitUtil.check(flags,6)) {
          buf.readUnsignedShort();
        }
        if (BitUtil.check(flags,7)) {
          getDeviceSession(channel,remoteAddress,buf.readBytes(15).toString(StandardCharsets.US_ASCII));
        }
        response=ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN,0);
        response.writeByte(0);
        sendResponse(channel,PT_APPDATA,index,serviceType,MSG_RESULT_CODE,response);
      }
 else       if (type == MSG_POS_DATA) {
        position.setTime(new Date((buf.readUnsignedInt() + 1262304000) * 1000));
        position.setLatitude(buf.readUnsignedInt() * 90.0 / 0xFFFFFFFFL);
        position.setLongitude(buf.readUnsignedInt() * 180.0 / 0xFFFFFFFFL);
        int flags=buf.readUnsignedByte();
        position.setValid(BitUtil.check(flags,0));
        if (BitUtil.check(flags,5)) {
          position.setLatitude(-position.getLatitude());
        }
        if (BitUtil.check(flags,6)) {
          position.setLongitude(-position.getLongitude());
        }
        int speed=buf.readUnsignedShort();
        position.setSpeed(BitUtil.to(speed,14));
        position.setCourse(buf.readUnsignedByte() + (BitUtil.check(speed,15) ? 0x100 : 0));
        position.set(Position.KEY_ODOMETER,buf.readUnsignedMedium() * 100);
        position.set(Position.KEY_INPUT,buf.readUnsignedByte());
        position.set(Position.KEY_EVENT,buf.readUnsignedByte());
        if (BitUtil.check(flags,7)) {
          position.setAltitude(buf.readMedium());
        }
      }
 else       if (type == MSG_EXT_POS_DATA) {
        int flags=buf.readUnsignedByte();
        if (BitUtil.check(flags,0)) {
          position.set(Position.KEY_VDOP,buf.readUnsignedShort());
        }
        if (BitUtil.check(flags,1)) {
          position.set(Position.KEY_HDOP,buf.readUnsignedShort());
        }
        if (BitUtil.check(flags,2)) {
          position.set(Position.KEY_PDOP,buf.readUnsignedShort());
        }
        if (BitUtil.check(flags,3)) {
          position.set(Position.KEY_SATELLITES,buf.readUnsignedByte());
        }
      }
 else       if (type == MSG_AD_SENSORS_DATA) {
        buf.readUnsignedByte();
        position.set(Position.KEY_OUTPUT,buf.readUnsignedByte());
        buf.readUnsignedByte();
      }
      buf.readerIndex(end);
    }
    if (deviceSession != null) {
      positions.add(position);
    }
  }
  return positions.isEmpty() ? null : positions;
}

Fixed version (ground truth):
@Override protected Object decode(Channel channel,SocketAddress remoteAddress,Object msg) throws Exception {
  ChannelBuffer buf=(ChannelBuffer)msg;
  buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3));
  List<Position> positions=new LinkedList<>();
  while (buf.readableBytes() > 2) {
    int length=buf.readUnsignedShort();
    int index=buf.readUnsignedShort();
    int recordFlags=buf.readUnsignedByte();
    if (BitUtil.check(recordFlags,0)) {
      buf.readUnsignedInt();
    }
    if (BitUtil.check(recordFlags,1)) {
      buf.readUnsignedInt();
    }
    if (BitUtil.check(recordFlags,2)) {
      buf.readUnsignedInt();
    }
    int serviceType=buf.readUnsignedByte();
    buf.readUnsignedByte();
    int recordEnd=buf.readerIndex() + length;
    Position position=new Position(getProtocolName());
    DeviceSession deviceSession=getDeviceSession(channel,remoteAddress);
    if (deviceSession != null) {
      position.setDeviceId(deviceSession.getDeviceId());
    }
    ChannelBuffer response=ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN,0);
    response.writeShort(index);
    response.writeByte(0);
    sendResponse(channel,PT_RESPONSE,index,serviceType,MSG_RECORD_RESPONSE,response);
    while (buf.readerIndex() < recordEnd) {
      int type=buf.readUnsignedByte();
      int end=buf.readUnsignedShort() + buf.readerIndex();
      if (type == MSG_TERM_IDENTITY) {
        buf.readUnsignedInt();
        int flags=buf.readUnsignedByte();
        if (BitUtil.check(flags,0)) {
          buf.readUnsignedShort();
        }
        if (BitUtil.check(flags,1)) {
          getDeviceSession(channel,remoteAddress,buf.readBytes(15).toString(StandardCharsets.US_ASCII).trim());
        }
        if (BitUtil.check(flags,2)) {
          getDeviceSession(channel,remoteAddress,buf.readBytes(16).toString(StandardCharsets.US_ASCII).trim());
        }
        if (BitUtil.check(flags,3)) {
          buf.skipBytes(3);
        }
        if (BitUtil.check(flags,5)) {
          buf.skipBytes(3);
        }
        if (BitUtil.check(flags,6)) {
          buf.readUnsignedShort();
        }
        if (BitUtil.check(flags,7)) {
          getDeviceSession(channel,remoteAddress,buf.readBytes(15).toString(StandardCharsets.US_ASCII).trim());
        }
        response=ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN,0);
        response.writeByte(0);
        sendResponse(channel,PT_APPDATA,index,serviceType,MSG_RESULT_CODE,response);
      }
 else       if (type == MSG_POS_DATA) {
        position.setTime(new Date((buf.readUnsignedInt() + 1262304000) * 1000));
        position.setLatitude(buf.readUnsignedInt() * 90.0 / 0xFFFFFFFFL);
        position.setLongitude(buf.readUnsignedInt() * 180.0 / 0xFFFFFFFFL);
        int flags=buf.readUnsignedByte();
        position.setValid(BitUtil.check(flags,0));
        if (BitUtil.check(flags,5)) {
          position.setLatitude(-position.getLatitude());
        }
        if (BitUtil.check(flags,6)) {
          position.setLongitude(-position.getLongitude());
        }
        int speed=buf.readUnsignedShort();
        position.setSpeed(BitUtil.to(speed,14));
        position.setCourse(buf.readUnsignedByte() + (BitUtil.check(speed,15) ? 0x100 : 0));
        position.set(Position.KEY_ODOMETER,buf.readUnsignedMedium() * 100);
        position.set(Position.KEY_INPUT,buf.readUnsignedByte());
        position.set(Position.KEY_EVENT,buf.readUnsignedByte());
        if (BitUtil.check(flags,7)) {
          position.setAltitude(buf.readMedium());
        }
      }
 else       if (type == MSG_EXT_POS_DATA) {
        int flags=buf.readUnsignedByte();
        if (BitUtil.check(flags,0)) {
          position.set(Position.KEY_VDOP,buf.readUnsignedShort());
        }
        if (BitUtil.check(flags,1)) {
          position.set(Position.KEY_HDOP,buf.readUnsignedShort());
        }
        if (BitUtil.check(flags,2)) {
          position.set(Position.KEY_PDOP,buf.readUnsignedShort());
        }
        if (BitUtil.check(flags,3)) {
          position.set(Position.KEY_SATELLITES,buf.readUnsignedByte());
        }
      }
 else       if (type == MSG_AD_SENSORS_DATA) {
        buf.readUnsignedByte();
        position.set(Position.KEY_OUTPUT,buf.readUnsignedByte());
        buf.readUnsignedByte();
      }
      buf.readerIndex(end);
    }
    if (serviceType == SERVICE_TELEDATA && deviceSession != null) {
      positions.add(position);
    }
  }
  return positions.isEmpty() ? null : positions;
}