Program: 193477
Project: 193477
Buggy source:
private void setImplLocked(Alarm a,boolean rebatching,boolean doValidate){
  if ((a.flags & AlarmManager.FLAG_IDLE_UNTIL) != 0) {
    if (mNextWakeFromIdle != null) {
      a.when=a.whenElapsed=a.maxWhenElapsed=mNextWakeFromIdle.whenElapsed;
    }
    final long nowElapsed=SystemClock.elapsedRealtime();
    final int fuzz=fuzzForDuration(a.whenElapsed - nowElapsed);
    if (fuzz > 0) {
      if (mRandom == null) {
        mRandom=new Random();
      }
      final int delta=mRandom.nextInt(fuzz);
      a.whenElapsed-=delta;
      if (false) {
        Slog.d(TAG,"String_Node_Str" + a.whenElapsed);
        Slog.d(TAG,"String_Node_Str" + (a.whenElapsed - nowElapsed));
        Slog.d(TAG,"String_Node_Str" + fuzz);
        Slog.d(TAG,"String_Node_Str" + delta);
        Slog.d(TAG,"String_Node_Str" + a.whenElapsed);
      }
      a.when=a.maxWhenElapsed=a.whenElapsed;
    }
  }
 else   if (mPendingIdleUntil != null) {
    if ((a.flags & (AlarmManager.FLAG_ALLOW_WHILE_IDLE | AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED | AlarmManager.FLAG_WAKE_FROM_IDLE)) == 0) {
      mPendingWhileIdleAlarms.add(a);
      return;
    }
  }
  int whichBatch=((a.flags & AlarmManager.FLAG_STANDALONE) != 0) ? -1 : attemptCoalesceLocked(a.whenElapsed,a.maxWhenElapsed);
  if (whichBatch < 0) {
    Batch batch=new Batch(a);
    addBatchLocked(mAlarmBatches,batch);
  }
 else {
    Batch batch=mAlarmBatches.get(whichBatch);
    if (batch.add(a)) {
      mAlarmBatches.remove(whichBatch);
      addBatchLocked(mAlarmBatches,batch);
    }
  }
  if (a.alarmClock != null) {
    mNextAlarmClockMayChange=true;
  }
  boolean needRebatch=false;
  if ((a.flags & AlarmManager.FLAG_IDLE_UNTIL) != 0) {
    mPendingIdleUntil=a;
    mConstants.updateAllowWhileIdleMinTimeLocked();
    needRebatch=true;
  }
 else   if ((a.flags & AlarmManager.FLAG_WAKE_FROM_IDLE) != 0) {
    if (mNextWakeFromIdle == null || mNextWakeFromIdle.whenElapsed > a.whenElapsed) {
      mNextWakeFromIdle=a;
      if (mPendingIdleUntil != null) {
        needRebatch=true;
      }
    }
  }
  if (!rebatching) {
    if (DEBUG_VALIDATE) {
      if (doValidate && !validateConsistencyLocked()) {
        Slog.v(TAG,"String_Node_Str" + a.type + "String_Node_Str"+ a.when+ "String_Node_Str"+ Long.toHexString(a.when)+ "String_Node_Str"+ a.whenElapsed+ "String_Node_Str"+ a.maxWhenElapsed+ "String_Node_Str"+ a.repeatInterval+ "String_Node_Str"+ a.operation+ "String_Node_Str"+ Integer.toHexString(a.flags));
        rebatchAllAlarmsLocked(false);
        needRebatch=false;
      }
    }
    if (needRebatch) {
      rebatchAllAlarmsLocked(false);
    }
    rescheduleKernelAlarmsLocked();
    updateNextAlarmClockLocked();
  }
}

Fixed version (ground truth):
private void setImplLocked(Alarm a,boolean rebatching,boolean doValidate){
  if ((a.flags & AlarmManager.FLAG_IDLE_UNTIL) != 0) {
    if (mNextWakeFromIdle != null && a.whenElapsed > mNextWakeFromIdle.whenElapsed) {
      a.when=a.whenElapsed=a.maxWhenElapsed=mNextWakeFromIdle.whenElapsed;
    }
    final long nowElapsed=SystemClock.elapsedRealtime();
    final int fuzz=fuzzForDuration(a.whenElapsed - nowElapsed);
    if (fuzz > 0) {
      if (mRandom == null) {
        mRandom=new Random();
      }
      final int delta=mRandom.nextInt(fuzz);
      a.whenElapsed-=delta;
      if (false) {
        Slog.d(TAG,"String_Node_Str" + a.whenElapsed);
        Slog.d(TAG,"String_Node_Str" + (a.whenElapsed - nowElapsed));
        Slog.d(TAG,"String_Node_Str" + fuzz);
        Slog.d(TAG,"String_Node_Str" + delta);
        Slog.d(TAG,"String_Node_Str" + a.whenElapsed);
      }
      a.when=a.maxWhenElapsed=a.whenElapsed;
    }
  }
 else   if (mPendingIdleUntil != null) {
    if ((a.flags & (AlarmManager.FLAG_ALLOW_WHILE_IDLE | AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED | AlarmManager.FLAG_WAKE_FROM_IDLE)) == 0) {
      mPendingWhileIdleAlarms.add(a);
      return;
    }
  }
  int whichBatch=((a.flags & AlarmManager.FLAG_STANDALONE) != 0) ? -1 : attemptCoalesceLocked(a.whenElapsed,a.maxWhenElapsed);
  if (whichBatch < 0) {
    Batch batch=new Batch(a);
    addBatchLocked(mAlarmBatches,batch);
  }
 else {
    Batch batch=mAlarmBatches.get(whichBatch);
    if (batch.add(a)) {
      mAlarmBatches.remove(whichBatch);
      addBatchLocked(mAlarmBatches,batch);
    }
  }
  if (a.alarmClock != null) {
    mNextAlarmClockMayChange=true;
  }
  boolean needRebatch=false;
  if ((a.flags & AlarmManager.FLAG_IDLE_UNTIL) != 0) {
    mPendingIdleUntil=a;
    mConstants.updateAllowWhileIdleMinTimeLocked();
    needRebatch=true;
  }
 else   if ((a.flags & AlarmManager.FLAG_WAKE_FROM_IDLE) != 0) {
    if (mNextWakeFromIdle == null || mNextWakeFromIdle.whenElapsed > a.whenElapsed) {
      mNextWakeFromIdle=a;
      if (mPendingIdleUntil != null) {
        needRebatch=true;
      }
    }
  }
  if (!rebatching) {
    if (DEBUG_VALIDATE) {
      if (doValidate && !validateConsistencyLocked()) {
        Slog.v(TAG,"String_Node_Str" + a.type + "String_Node_Str"+ a.when+ "String_Node_Str"+ Long.toHexString(a.when)+ "String_Node_Str"+ a.whenElapsed+ "String_Node_Str"+ a.maxWhenElapsed+ "String_Node_Str"+ a.repeatInterval+ "String_Node_Str"+ a.operation+ "String_Node_Str"+ Integer.toHexString(a.flags));
        rebatchAllAlarmsLocked(false);
        needRebatch=false;
      }
    }
    if (needRebatch) {
      rebatchAllAlarmsLocked(false);
    }
    rescheduleKernelAlarmsLocked();
    updateNextAlarmClockLocked();
  }
}