Program: 34235
Project: 34235
Buggy source:
/** 
 * Runs periodic background processing. <br><br> This method will first check whether there are any QueueRecord objects saved in the database. If there are, it will attempt to complete the task recorded by each of those QueueRecords in turn. After that, it will run the 'check for messages' task. If no QueueRecords are found in the database, it will run the 'check for messages' task. 
 */
private void processTasks(){
  Log.i(TAG,"String_Node_Str");
  TaskController taskController=new TaskController();
  QueueRecordProvider queueProv=QueueRecordProvider.get(getApplicationContext());
  QueueRecordProcessor queueProc=new QueueRecordProcessor();
  ArrayList<QueueRecord> queueRecords=queueProv.getAllQueueRecords();
  Log.i(TAG,"String_Node_Str" + queueRecords.size());
  if (queueRecords.size() > 0) {
    Collections.sort(queueRecords);
    for (    QueueRecord q : queueRecords) {
      try {
        Log.i(TAG,"String_Node_Str" + q.getTask() + "String_Node_Str"+ q.getAttempts());
        int attempts=q.getAttempts();
        String task=q.getTask();
        if (attempts > MAXIMUM_ATTEMPTS) {
          Log.d(TAG,"String_Node_Str" + task + "String_Node_Str"+ attempts+ "String_Node_Str");
          if (task.equals(TASK_SEND_MESSAGE)) {
            MessageProvider msgProv=MessageProvider.get(getApplicationContext());
            Message messageToSend=msgProv.searchForSingleRecord(q.getObject0Id());
            MessageStatusHandler.updateMessageStatus(messageToSend,getApplicationContext().getString(R.string.message_status_sending_failed));
          }
          queueProc.deleteQueueRecord(q);
          continue;
        }
        if (task.equals(TASK_SEND_MESSAGE)) {
          try {
            MessageProvider msgProv=MessageProvider.get(getApplicationContext());
            Message messageToSend=msgProv.searchForSingleRecord(q.getObject0Id());
            if (checkForEarlierSendMsgQueueRecords(q)) {
              continue;
            }
            long currentTime=System.currentTimeMillis() / 1000;
            if (q.getTriggerTime() > currentTime) {
              Log.i(TAG,"String_Node_Str" + q.getTask() + "String_Node_Str"+ "String_Node_Str"+ TimeUtils.getTimeMessage(q.getTriggerTime() - currentTime));
              continue;
            }
            if (q.getRecordCount() == 0) {
              taskController.sendMessage(q,messageToSend,DO_POW,FIRST_ATTEMPT_TTL,FIRST_ATTEMPT_TTL);
            }
 else {
              currentTime=System.currentTimeMillis() / 1000;
              queueProc.createAndSaveQueueRecord(TASK_SEND_MESSAGE,currentTime + SUBSEQUENT_ATTEMPTS_TTL,q.getRecordCount() + 1,messageToSend,null,null);
              taskController.sendMessage(q,messageToSend,DO_POW,SUBSEQUENT_ATTEMPTS_TTL,SUBSEQUENT_ATTEMPTS_TTL);
            }
          }
 catch (          RuntimeException e) {
            Log.i(TAG,"String_Node_Str" + TASK_SEND_MESSAGE + "String_Node_Str"+ "String_Node_Str");
            queueProv.deleteQueueRecord(q);
            continue;
          }
        }
 else         if (task.equals(TASK_PROCESS_OUTGOING_MESSAGE)) {
          Message messageToSend=null;
          try {
            MessageProvider msgProv=MessageProvider.get(getApplicationContext());
            messageToSend=msgProv.searchForSingleRecord(q.getObject0Id());
          }
 catch (          RuntimeException e) {
            Log.i(TAG,"String_Node_Str" + TASK_PROCESS_OUTGOING_MESSAGE + "String_Node_Str"+ "String_Node_Str");
            queueProv.deleteQueueRecord(q);
            continue;
          }
          PubkeyProvider pubProv=PubkeyProvider.get(App.getContext());
          Pubkey toPubkey=pubProv.searchForSingleRecord(q.getObject1Id());
          if (q.getRecordCount() == 0) {
            taskController.processOutgoingMessage(q,messageToSend,toPubkey,DO_POW,FIRST_ATTEMPT_TTL);
          }
 else {
            taskController.processOutgoingMessage(q,messageToSend,toPubkey,DO_POW,SUBSEQUENT_ATTEMPTS_TTL);
          }
        }
 else         if (task.equals(TASK_DISSEMINATE_MESSAGE)) {
          PayloadProvider payProv=PayloadProvider.get(getApplicationContext());
          Payload msgPayload=payProv.searchForSingleRecord(q.getObject1Id());
          boolean msgValid=new ObjectProcessor().validateObject(msgPayload.getPayload());
          if (msgValid == false) {
            Log.d(TAG,"String_Node_Str" + "String_Node_Str");
            payProv.deletePayload(msgPayload);
            queueProv.deleteQueueRecord(q);
            MessageProvider msgProv=MessageProvider.get(getApplicationContext());
            Message messageToSend=msgProv.searchForSingleRecord(q.getObject0Id());
            PubkeyProvider pubProv=PubkeyProvider.get(App.getContext());
            Pubkey toPubkey=pubProv.searchForSingleRecord(q.getObject2Id());
            queueProc.createAndSaveQueueRecord(BackgroundService.TASK_PROCESS_OUTGOING_MESSAGE,0,q.getRecordCount(),messageToSend,toPubkey,null);
            continue;
          }
          if (NetworkHelper.checkInternetAvailability() == true) {
            PubkeyProvider pubProv=PubkeyProvider.get(App.getContext());
            Pubkey toPubkey=pubProv.searchForSingleRecord(q.getObject2Id());
            taskController.disseminateMessage(q,msgPayload,toPubkey,DO_POW);
          }
 else {
            MessageProvider messageProv=MessageProvider.get(getApplicationContext());
            Message messageToSend=messageProv.searchForSingleRecord(q.getObject0Id());
            MessageStatusHandler.updateMessageStatus(messageToSend,getApplicationContext().getString(R.string.message_status_waiting_for_connection));
          }
        }
 else         if (task.equals(TASK_CREATE_IDENTITY)) {
          taskController.createIdentity(q,DO_POW);
        }
 else         if (task.equals(TASK_DISSEMINATE_PUBKEY)) {
          PayloadProvider payProv=PayloadProvider.get(getApplicationContext());
          Payload pubkeyPayload=payProv.searchForSingleRecord(q.getObject0Id());
          boolean pubkeyValid=new ObjectProcessor().validateObject(pubkeyPayload.getPayload());
          if (pubkeyValid) {
            if (NetworkHelper.checkInternetAvailability() == true) {
              taskController.disseminatePubkey(q,pubkeyPayload,DO_POW);
            }
          }
 else {
            Log.d(TAG,"String_Node_Str" + "String_Node_Str");
            queueProv.deleteQueueRecord(q);
            AddressProvider addProv=AddressProvider.get(getApplicationContext());
            Address address=addProv.searchForSingleRecord(pubkeyPayload.getRelatedAddressId());
            queueProc.createAndSaveQueueRecord(TASK_CREATE_IDENTITY,0,q.getRecordCount(),address,null,null);
          }
        }
 else {
          Log.e(TAG,"String_Node_Str" + "String_Node_Str" + task);
        }
      }
 catch (      Exception e) {
        Log.e(TAG,"String_Node_Str" + e.getMessage());
        queueProv.deleteQueueRecord(q);
      }
    }
    runPeriodicTasks();
  }
 else {
    runPeriodicTasks();
    if (checkIfDatabaseCleaningIsRequired()) {
      Intent intent=new Intent(getBaseContext(),DatabaseCleaningService.class);
      intent.putExtra(DatabaseCleaningService.EXTRA_RUN_DATABASE_CLEANING_ROUTINE,true);
      startService(intent);
    }
  }
}

Fixed version (ground truth):
/** 
 * Runs periodic background processing. <br><br> This method will first check whether there are any QueueRecord objects saved in the database. If there are, it will attempt to complete the task recorded by each of those QueueRecords in turn. After that, it will run the 'check for messages' task. If no QueueRecords are found in the database, it will run the 'check for messages' task. 
 */
private void processTasks(){
  Log.i(TAG,"String_Node_Str");
  TaskController taskController=new TaskController();
  QueueRecordProvider queueProv=QueueRecordProvider.get(getApplicationContext());
  QueueRecordProcessor queueProc=new QueueRecordProcessor();
  ArrayList<QueueRecord> queueRecords=queueProv.getAllQueueRecords();
  Log.i(TAG,"String_Node_Str" + queueRecords.size());
  if (queueRecords.size() > 0) {
    Collections.sort(queueRecords);
    for (    QueueRecord q : queueRecords) {
      try {
        Log.i(TAG,"String_Node_Str" + q.getTask() + "String_Node_Str"+ q.getAttempts());
        int attempts=q.getAttempts();
        String task=q.getTask();
        if (attempts > MAXIMUM_ATTEMPTS) {
          Log.d(TAG,"String_Node_Str" + task + "String_Node_Str"+ attempts+ "String_Node_Str");
          if (task.equals(TASK_SEND_MESSAGE)) {
            MessageProvider msgProv=MessageProvider.get(getApplicationContext());
            Message messageToSend=msgProv.searchForSingleRecord(q.getObject0Id());
            MessageStatusHandler.updateMessageStatus(messageToSend,getApplicationContext().getString(R.string.message_status_sending_failed));
          }
          queueProc.deleteQueueRecord(q);
          continue;
        }
 else         if (task.equals(TASK_SEND_MESSAGE)) {
          try {
            MessageProvider msgProv=MessageProvider.get(getApplicationContext());
            Message messageToSend=msgProv.searchForSingleRecord(q.getObject0Id());
            if (checkForEarlierSendMsgQueueRecords(q)) {
              continue;
            }
 else {
              if (q.getRecordCount() != 0) {
                q.setRecordCount(0);
                queueProc.updateQueueRecord(q);
              }
            }
            long currentTime=System.currentTimeMillis() / 1000;
            if (q.getTriggerTime() > currentTime) {
              Log.i(TAG,"String_Node_Str" + q.getTask() + "String_Node_Str"+ "String_Node_Str"+ TimeUtils.getTimeMessage(q.getTriggerTime() - currentTime));
              continue;
            }
            if (q.getRecordCount() == 0) {
              taskController.sendMessage(q,messageToSend,DO_POW,FIRST_ATTEMPT_TTL,FIRST_ATTEMPT_TTL);
            }
 else {
              currentTime=System.currentTimeMillis() / 1000;
              queueProc.createAndSaveQueueRecord(TASK_SEND_MESSAGE,currentTime + SUBSEQUENT_ATTEMPTS_TTL,q.getRecordCount() + 1,messageToSend,null,null);
              taskController.sendMessage(q,messageToSend,DO_POW,SUBSEQUENT_ATTEMPTS_TTL,SUBSEQUENT_ATTEMPTS_TTL);
            }
          }
 catch (          RuntimeException e) {
            Log.i(TAG,"String_Node_Str" + TASK_SEND_MESSAGE + "String_Node_Str"+ "String_Node_Str");
            queueProv.deleteQueueRecord(q);
            continue;
          }
        }
 else         if (task.equals(TASK_PROCESS_OUTGOING_MESSAGE)) {
          Message messageToSend=null;
          try {
            MessageProvider msgProv=MessageProvider.get(getApplicationContext());
            messageToSend=msgProv.searchForSingleRecord(q.getObject0Id());
          }
 catch (          RuntimeException e) {
            Log.i(TAG,"String_Node_Str" + TASK_PROCESS_OUTGOING_MESSAGE + "String_Node_Str"+ "String_Node_Str");
            queueProv.deleteQueueRecord(q);
            continue;
          }
          PubkeyProvider pubProv=PubkeyProvider.get(App.getContext());
          Pubkey toPubkey=pubProv.searchForSingleRecord(q.getObject1Id());
          if (q.getRecordCount() == 0) {
            taskController.processOutgoingMessage(q,messageToSend,toPubkey,DO_POW,FIRST_ATTEMPT_TTL);
          }
 else {
            taskController.processOutgoingMessage(q,messageToSend,toPubkey,DO_POW,SUBSEQUENT_ATTEMPTS_TTL);
          }
        }
 else         if (task.equals(TASK_DISSEMINATE_MESSAGE)) {
          PayloadProvider payProv=PayloadProvider.get(getApplicationContext());
          Payload msgPayload=payProv.searchForSingleRecord(q.getObject1Id());
          boolean msgValid=new ObjectProcessor().validateObject(msgPayload.getPayload());
          if (msgValid == false) {
            Log.d(TAG,"String_Node_Str" + "String_Node_Str");
            payProv.deletePayload(msgPayload);
            queueProv.deleteQueueRecord(q);
            MessageProvider msgProv=MessageProvider.get(getApplicationContext());
            Message messageToSend=msgProv.searchForSingleRecord(q.getObject0Id());
            PubkeyProvider pubProv=PubkeyProvider.get(App.getContext());
            Pubkey toPubkey=pubProv.searchForSingleRecord(q.getObject2Id());
            queueProc.createAndSaveQueueRecord(BackgroundService.TASK_PROCESS_OUTGOING_MESSAGE,0,q.getRecordCount(),messageToSend,toPubkey,null);
            continue;
          }
          if (NetworkHelper.checkInternetAvailability() == true) {
            PubkeyProvider pubProv=PubkeyProvider.get(App.getContext());
            Pubkey toPubkey=pubProv.searchForSingleRecord(q.getObject2Id());
            taskController.disseminateMessage(q,msgPayload,toPubkey,DO_POW);
          }
 else {
            MessageProvider messageProv=MessageProvider.get(getApplicationContext());
            Message messageToSend=messageProv.searchForSingleRecord(q.getObject0Id());
            MessageStatusHandler.updateMessageStatus(messageToSend,getApplicationContext().getString(R.string.message_status_waiting_for_connection));
          }
        }
 else         if (task.equals(TASK_CREATE_IDENTITY)) {
          taskController.createIdentity(q,DO_POW);
        }
 else         if (task.equals(TASK_DISSEMINATE_PUBKEY)) {
          PayloadProvider payProv=PayloadProvider.get(getApplicationContext());
          Payload pubkeyPayload=payProv.searchForSingleRecord(q.getObject0Id());
          boolean pubkeyValid=new ObjectProcessor().validateObject(pubkeyPayload.getPayload());
          if (pubkeyValid) {
            if (NetworkHelper.checkInternetAvailability() == true) {
              taskController.disseminatePubkey(q,pubkeyPayload,DO_POW);
            }
          }
 else {
            Log.d(TAG,"String_Node_Str" + "String_Node_Str");
            queueProv.deleteQueueRecord(q);
            AddressProvider addProv=AddressProvider.get(getApplicationContext());
            Address address=addProv.searchForSingleRecord(pubkeyPayload.getRelatedAddressId());
            queueProc.createAndSaveQueueRecord(TASK_CREATE_IDENTITY,0,q.getRecordCount(),address,null,null);
          }
        }
 else {
          Log.e(TAG,"String_Node_Str" + "String_Node_Str" + task);
        }
      }
 catch (      Exception e) {
        Log.e(TAG,"String_Node_Str" + e.getMessage());
        queueProv.deleteQueueRecord(q);
      }
    }
    runPeriodicTasks();
  }
 else {
    runPeriodicTasks();
    if (checkIfDatabaseCleaningIsRequired()) {
      Intent intent=new Intent(getBaseContext(),DatabaseCleaningService.class);
      intent.putExtra(DatabaseCleaningService.EXTRA_RUN_DATABASE_CLEANING_ROUTINE,true);
      startService(intent);
    }
  }
}