Program: 34387
Project: 34387
Buggy source:
/** 
 * Clear all the request handlers - this is called when unmanage request is called by the agent Do a bulk unsubscribe to improve performance
 * @param dmClient
 * @throws MqttException 
 */
public static void clearRequestHandlers(ManagedClient dmClient) throws MqttException {
  String[] topics=new String[7];
  IMqttMessageListener[] listener=new IMqttMessageListener[7];
  int index=0;
  DeviceUpdateRequestHandler device=deviceUpdateHandlers.remove(dmClient);
  if (device != null) {
    topics[index]=device.getTopic();
    listener[index++]=device;
  }
  ObserveRequestHandler observe=observeHandlers.remove(dmClient);
  if (observe != null) {
    topics[index]=observe.getTopic();
    listener[index++]=observe;
  }
  CancelRequestHandler cancel=cancelHandlers.remove(dmClient);
  if (cancel != null) {
    topics[index]=cancel.getTopic();
    listener[index++]=cancel;
  }
  RebootRequestHandler reboot=rebootHandlers.remove(dmClient);
  if (reboot != null) {
    topics[index]=reboot.getTopic();
    listener[index++]=reboot;
  }
  FactoryResetRequestHandler reset=resetHandlers.remove(dmClient);
  if (reset != null) {
    topics[index]=reset.getTopic();
    listener[index++]=reset;
  }
  FirmwareDownloadRequestHandler fwDownload=fwDownloadHandlers.remove(dmClient);
  if (fwDownload != null) {
    topics[index]=fwDownload.getTopic();
    listener[index++]=fwDownload;
  }
  FirmwareUpdateRequestHandler fwUpdate=fwUpdateHandlers.remove(dmClient);
  if (fwUpdate != null) {
    topics[index]=fwUpdate.getTopic();
    listener[index++]=fwUpdate;
  }
  if (index > 0) {
    int[] qos=new int[index];
    Arrays.fill(qos,1);
    dmClient.unsubscribe(topics);
  }
}

Fixed version (ground truth):
/** 
 * Clear all the request handlers - this is called when unmanage request is called by the agent Do a bulk unsubscribe to improve performance
 * @param dmClient The Managed device instance
 * @throws MqttException failure in unsubscription
 */
public static void clearRequestHandlers(ManagedClient dmClient) throws MqttException {
  String[] topics=new String[7];
  IMqttMessageListener[] listener=new IMqttMessageListener[7];
  int index=0;
  DeviceUpdateRequestHandler device=deviceUpdateHandlers.remove(dmClient);
  if (device != null) {
    topics[index]=device.getTopic();
    listener[index++]=device;
  }
  ObserveRequestHandler observe=observeHandlers.remove(dmClient);
  if (observe != null) {
    topics[index]=observe.getTopic();
    listener[index++]=observe;
  }
  CancelRequestHandler cancel=cancelHandlers.remove(dmClient);
  if (cancel != null) {
    topics[index]=cancel.getTopic();
    listener[index++]=cancel;
  }
  RebootRequestHandler reboot=rebootHandlers.remove(dmClient);
  if (reboot != null) {
    topics[index]=reboot.getTopic();
    listener[index++]=reboot;
  }
  FactoryResetRequestHandler reset=resetHandlers.remove(dmClient);
  if (reset != null) {
    topics[index]=reset.getTopic();
    listener[index++]=reset;
  }
  FirmwareDownloadRequestHandler fwDownload=fwDownloadHandlers.remove(dmClient);
  if (fwDownload != null) {
    topics[index]=fwDownload.getTopic();
    listener[index++]=fwDownload;
  }
  FirmwareUpdateRequestHandler fwUpdate=fwUpdateHandlers.remove(dmClient);
  if (fwUpdate != null) {
    topics[index]=fwUpdate.getTopic();
    listener[index++]=fwUpdate;
  }
  if (index > 0) {
    int[] qos=new int[index];
    Arrays.fill(qos,1);
    dmClient.unsubscribe(topics);
  }
}