Program: 142237
Project: 142237
Buggy source:
/** 
 * Process user returning home from the form entry activity. Triggers form submission cycle, cleans up some session state.
 * @param resultCode exit code of form entry activity
 * @param intent points to form instance
 */
private void processReturnFromFormEntry(int resultCode,Intent intent){
  AndroidSessionWrapper currentState=CommCareApplication._().getCurrentSessionWrapper();
  FormRecord current=currentState.getFormRecord();
  if (FormRecord.STATUS_COMPLETE.equals(current.getStatus()) || FormRecord.STATUS_SAVED.equals(current.getStatus()) || FormRecord.STATUS_UNSENT.equals(current.getStatus())) {
    currentState.reset();
    if (wasExternal) {
      this.finish();
    }
 else {
      goToFormArchive(false,current);
    }
    return;
  }
  if (resultCode == RESULT_OK) {
    Uri resultInstanceURI=intent.getData();
    if (resultInstanceURI == null) {
      CommCareApplication._().reportNotificationMessage(NotificationMessageFactory.message(StockMessages.FormEntry_Unretrievable));
      Toast.makeText(this,"String_Node_Str",Toast.LENGTH_LONG);
      Logger.log(AndroidLogger.TYPE_ERROR_WORKFLOW,"String_Node_Str");
      clearSessionAndExit(currentState);
      return;
    }
    Cursor c=getContentResolver().query(resultInstanceURI,null,null,null,null);
    if (!c.moveToFirst()) {
      throw new IllegalArgumentException("String_Node_Str");
    }
    String instanceStatus=c.getString(c.getColumnIndexOrThrow(InstanceProviderAPI.InstanceColumns.STATUS));
    boolean complete=InstanceProviderAPI.STATUS_COMPLETE.equals(instanceStatus);
    if (complete) {
      checkAndStartUnsentTask(false);
      refreshView();
      if (wasExternal) {
        this.finish();
      }
      if (!currentState.terminateSession()) {
        return;
      }
    }
 else {
      clearSessionAndExit(currentState);
      return;
    }
  }
 else   if (resultCode == RESULT_CANCELED) {
    Logger.log(AndroidLogger.TYPE_FORM_ENTRY,"String_Node_Str");
    if (current.getStatus() == FormRecord.STATUS_UNSTARTED) {
      FormRecordCleanupTask.wipeRecord(this,currentState);
    }
    currentState.reset();
    if (wasExternal) {
      this.finish();
      return;
    }
 else     if (current.getStatus().equals(FormRecord.STATUS_INCOMPLETE)) {
      goToFormArchive(true,current);
      return;
    }
 else {
      currentState.getSession().stepBack();
      currentState.setFormRecordId(-1);
    }
  }
}

Fixed version (ground truth):
/** 
 * Process user returning home from the form entry activity. Triggers form submission cycle, cleans up some session state.
 * @param resultCode exit code of form entry activity
 * @param intent points to form instance
 */
private void processReturnFromFormEntry(int resultCode,Intent intent){
  AndroidSessionWrapper currentState=CommCareApplication._().getCurrentSessionWrapper();
  FormRecord current=currentState.getFormRecord();
  if (current == null) {
    Toast.makeText(this,"String_Node_Str",Toast.LENGTH_LONG);
    Logger.log(AndroidLogger.TYPE_ERROR_WORKFLOW,"String_Node_Str");
    clearSessionAndExit(currentState);
  }
  if (FormRecord.STATUS_COMPLETE.equals(current.getStatus()) || FormRecord.STATUS_SAVED.equals(current.getStatus()) || FormRecord.STATUS_UNSENT.equals(current.getStatus())) {
    currentState.reset();
    if (wasExternal) {
      this.finish();
    }
 else {
      goToFormArchive(false,current);
    }
    return;
  }
  if (resultCode == RESULT_OK) {
    Uri resultInstanceURI=intent.getData();
    if (resultInstanceURI == null) {
      CommCareApplication._().reportNotificationMessage(NotificationMessageFactory.message(StockMessages.FormEntry_Unretrievable));
      Toast.makeText(this,"String_Node_Str",Toast.LENGTH_LONG);
      Logger.log(AndroidLogger.TYPE_ERROR_WORKFLOW,"String_Node_Str");
      clearSessionAndExit(currentState);
      return;
    }
    Cursor c=getContentResolver().query(resultInstanceURI,null,null,null,null);
    if (!c.moveToFirst()) {
      throw new IllegalArgumentException("String_Node_Str");
    }
    String instanceStatus=c.getString(c.getColumnIndexOrThrow(InstanceProviderAPI.InstanceColumns.STATUS));
    boolean complete=InstanceProviderAPI.STATUS_COMPLETE.equals(instanceStatus);
    if (complete) {
      checkAndStartUnsentTask(false);
      refreshView();
      if (wasExternal) {
        this.finish();
      }
      if (!currentState.terminateSession()) {
        return;
      }
    }
 else {
      clearSessionAndExit(currentState);
      return;
    }
  }
 else   if (resultCode == RESULT_CANCELED) {
    Logger.log(AndroidLogger.TYPE_FORM_ENTRY,"String_Node_Str");
    if (current.getStatus() == FormRecord.STATUS_UNSTARTED) {
      FormRecordCleanupTask.wipeRecord(this,currentState);
    }
    currentState.reset();
    if (wasExternal) {
      this.finish();
      return;
    }
 else     if (current.getStatus().equals(FormRecord.STATUS_INCOMPLETE)) {
      goToFormArchive(true,current);
      return;
    }
 else {
      currentState.getSession().stepBack();
      currentState.setFormRecordId(-1);
    }
  }
}