Program: 2741
Project: 2741
Buggy source:
/** 
 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
 * @param affinityTask The task we are looking for an affinity to.
 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
 */
private final void resetAffinityTaskIfNeededLocked(TaskRecord affinityTask,TaskRecord task,boolean topTaskIsHigher,boolean forceReset,ActivityRecord taskTop){
  int replyChainEnd=-1;
  final int taskId=task.taskId;
  final String taskAffinity=task.affinity;
  final ArrayList<ActivityRecord> activities=affinityTask.mActivities;
  final int numActivities=activities.size();
  for (int i=numActivities - 1; i > 0; --i) {
    ActivityRecord target=activities.get(i);
    final int flags=target.info.flags;
    boolean finishOnTaskLaunch=(flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
    boolean allowTaskReparenting=(flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
    if (target.resultTo != null) {
      if (replyChainEnd < 0) {
        replyChainEnd=i;
      }
    }
 else     if (topTaskIsHigher && allowTaskReparenting && taskAffinity != null && taskAffinity.equals(target.taskAffinity)) {
      if (forceReset || finishOnTaskLaunch) {
        final int start=replyChainEnd >= 0 ? replyChainEnd : i;
        if (DEBUG_TASKS)         Slog.v(TAG,"String_Node_Str" + start + "String_Node_Str"+ i);
        for (int srcPos=start; srcPos >= i; --srcPos) {
          final ActivityRecord p=activities.get(srcPos);
          if (p.finishing) {
            continue;
          }
          if (VALIDATE_TASK_REPLACE)           Slog.w(TAG,"String_Node_Str" + p);
          finishActivityLocked(p,Activity.RESULT_CANCELED,null,"String_Node_Str",false);
        }
      }
 else {
        int taskTopI=mHistory.indexOf(taskTop);
        final int end=replyChainEnd >= 0 ? replyChainEnd : i;
        if (DEBUG_TASKS)         Slog.v(TAG,"String_Node_Str" + i + "String_Node_Str"+ end);
        for (int srcPos=i; srcPos <= end; ++srcPos) {
          final ActivityRecord p=activities.get(srcPos);
          setTask(p,task,null,false);
          task.addActivityToTop(p);
{
            mHistory.remove(p);
            mHistory.add(taskTopI,p);
          }
          if (DEBUG_ADD_REMOVE)           Slog.i(TAG,"String_Node_Str" + p + "String_Node_Str"+ task,new RuntimeException("String_Node_Str").fillInStackTrace());
          if (DEBUG_TASKS)           Slog.v(TAG,"String_Node_Str" + p + "String_Node_Str"+ srcPos+ "String_Node_Str"+ task);
          mService.mWindowManager.setAppGroupId(p.appToken,taskId);
        }
        mService.mWindowManager.moveTaskToTop(taskId);
        if (VALIDATE_TASK_REPLACE) {
          verifyActivityRecords(false);
        }
        if (VALIDATE_TOKENS) {
          validateAppTokensLocked();
        }
        if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
          ArrayList<ActivityRecord> taskActivities=task.mActivities;
          boolean found=false;
          int targetNdx=taskActivities.indexOf(target);
          if (targetNdx > 0) {
            ActivityRecord p=taskActivities.get(targetNdx - 1);
            if (p.intent.getComponent().equals(target.intent.getComponent())) {
              if (finishActivityLocked(p,Activity.RESULT_CANCELED,null,"String_Node_Str",false)) {
                taskTopI--;
              }
            }
          }
        }
      }
      replyChainEnd=-1;
    }
  }
}

Fixed version (ground truth):
/** 
 * Helper method for #resetTaskIfNeededLocked. Processes all of the activities in a given TaskRecord looking for an affinity with the task of resetTaskIfNeededLocked.taskTop.
 * @param affinityTask The task we are looking for an affinity to.
 * @param task Task that resetTaskIfNeededLocked.taskTop belongs to.
 * @param topTaskIsHigher True if #task has already been processed by resetTaskIfNeededLocked.
 * @param forceReset Flag passed in to resetTaskIfNeededLocked.
 */
private final int resetAffinityTaskIfNeededLocked(TaskRecord affinityTask,TaskRecord task,boolean topTaskIsHigher,boolean forceReset,int taskInsertionPoint){
  int replyChainEnd=-1;
  final int taskId=task.taskId;
  final String taskAffinity=task.affinity;
  final ArrayList<ActivityRecord> activities=affinityTask.mActivities;
  final int numActivities=activities.size();
  for (int i=numActivities - 1; i > 0; --i) {
    ActivityRecord target=activities.get(i);
    final int flags=target.info.flags;
    boolean finishOnTaskLaunch=(flags & ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
    boolean allowTaskReparenting=(flags & ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
    if (target.resultTo != null) {
      if (replyChainEnd < 0) {
        replyChainEnd=i;
      }
    }
 else     if (topTaskIsHigher && allowTaskReparenting && taskAffinity != null && taskAffinity.equals(target.taskAffinity)) {
      if (forceReset || finishOnTaskLaunch) {
        final int start=replyChainEnd >= 0 ? replyChainEnd : i;
        if (DEBUG_TASKS)         Slog.v(TAG,"String_Node_Str" + start + "String_Node_Str"+ i);
        for (int srcPos=start; srcPos >= i; --srcPos) {
          final ActivityRecord p=activities.get(srcPos);
          if (p.finishing) {
            continue;
          }
          if (VALIDATE_TASK_REPLACE)           Slog.w(TAG,"String_Node_Str" + p);
          finishActivityLocked(p,Activity.RESULT_CANCELED,null,"String_Node_Str",false);
        }
      }
 else {
        if (taskInsertionPoint < 0) {
          taskInsertionPoint=task.mActivities.size();
        }
        int taskTopI=mHistory.indexOf(task.mActivities.get(taskInsertionPoint - 1));
        final int start=replyChainEnd >= 0 ? replyChainEnd : i;
        if (DEBUG_TASKS)         Slog.v(TAG,"String_Node_Str" + affinityTask + "String_Node_Str"+ start+ "String_Node_Str"+ i+ "String_Node_Str"+ task+ "String_Node_Str"+ taskInsertionPoint);
        for (int srcPos=start; srcPos >= i; --srcPos) {
          final ActivityRecord p=activities.get(srcPos);
          setTask(p,task,null,false);
          task.addActivityAtIndex(taskInsertionPoint,p);
{
            mHistory.remove(p);
            mHistory.add(taskTopI,p);
            if (DEBUG_ADD_REMOVE)             Slog.i(TAG,"String_Node_Str" + p + "String_Node_Str"+ taskTopI);
            --taskTopI;
          }
          if (DEBUG_ADD_REMOVE)           Slog.i(TAG,"String_Node_Str" + p + "String_Node_Str"+ task,new RuntimeException("String_Node_Str").fillInStackTrace());
          if (DEBUG_TASKS)           Slog.v(TAG,"String_Node_Str" + p + "String_Node_Str"+ srcPos+ "String_Node_Str"+ task);
          mService.mWindowManager.setAppGroupId(p.appToken,taskId);
        }
        mService.mWindowManager.moveTaskToTop(taskId);
        if (VALIDATE_TASK_REPLACE) {
          verifyActivityRecords(false);
        }
        if (VALIDATE_TOKENS) {
          validateAppTokensLocked();
        }
        if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
          ArrayList<ActivityRecord> taskActivities=task.mActivities;
          boolean found=false;
          int targetNdx=taskActivities.indexOf(target);
          if (targetNdx > 0) {
            ActivityRecord p=taskActivities.get(targetNdx - 1);
            if (p.intent.getComponent().equals(target.intent.getComponent())) {
              if (finishActivityLocked(p,Activity.RESULT_CANCELED,null,"String_Node_Str",false)) {
                taskTopI--;
              }
            }
          }
        }
      }
      replyChainEnd=-1;
    }
  }
  return taskInsertionPoint;
}