Program: 196705
Project: 196705
Buggy source:
void setOnBatteryLocked(final long mSecRealtime,final long mSecUptime,final boolean onBattery,final int oldStatus,final int level){
  boolean doWrite=false;
  Message m=mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
  m.arg1=onBattery ? 1 : 0;
  mHandler.sendMessage(m);
  mOnBattery=mOnBatteryInternal=onBattery;
  final long uptime=mSecUptime * 1000;
  final long realtime=mSecRealtime * 1000;
  final boolean screenOn=mScreenState == Display.STATE_ON;
  if (onBattery) {
    boolean reset=false;
    if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL || level >= 90 || getLowDischargeAmountSinceCharge() >= 60) || (getHighDischargeAmountSinceCharge() >= 60 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER)) {
      Slog.i(TAG,"String_Node_Str" + level + "String_Node_Str"+ oldStatus+ "String_Node_Str"+ getLowDischargeAmountSinceCharge()+ "String_Node_Str"+ getHighDischargeAmountSinceCharge());
      if (getLowDischargeAmountSinceCharge() >= 20) {
        final Parcel parcel=Parcel.obtain();
        writeSummaryToParcel(parcel,true);
        BackgroundThread.getHandler().post(new Runnable(){
          @Override public void run(){
synchronized (mCheckinFile) {
              FileOutputStream stream=null;
              try {
                stream=mCheckinFile.startWrite();
                stream.write(parcel.marshall());
                stream.flush();
                FileUtils.sync(stream);
                stream.close();
                mCheckinFile.finishWrite(stream);
              }
 catch (              IOException e) {
                Slog.w("String_Node_Str","String_Node_Str",e);
                mCheckinFile.failWrite(stream);
              }
 finally {
                parcel.recycle();
              }
            }
          }
        }
);
      }
      doWrite=true;
      resetAllStatsLocked();
      mDischargeStartLevel=level;
      reset=true;
      mNumDischargeStepDurations=0;
    }
    mLastDischargeStepLevel=level;
    mMinDischargeStepLevel=level;
    mLastDischargeStepTime=-1;
    mInitStepMode=mCurStepMode;
    mModStepMode=0;
    pullPendingStateUpdatesLocked();
    mHistoryCur.batteryLevel=(byte)level;
    mHistoryCur.states&=~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
    if (DEBUG_HISTORY)     Slog.v(TAG,"String_Node_Str" + Integer.toHexString(mHistoryCur.states));
    if (reset) {
      mRecordingHistory=true;
      startRecordingHistory(mSecRealtime,mSecUptime,reset);
    }
    addHistoryRecordLocked(mSecRealtime,mSecUptime);
    mDischargeCurrentLevel=mDischargeUnplugLevel=level;
    if (screenOn) {
      mDischargeScreenOnUnplugLevel=level;
      mDischargeScreenOffUnplugLevel=0;
    }
 else {
      mDischargeScreenOnUnplugLevel=0;
      mDischargeScreenOffUnplugLevel=level;
    }
    mDischargeAmountScreenOn=0;
    mDischargeAmountScreenOff=0;
    updateTimeBasesLocked(true,!screenOn,uptime,realtime);
  }
 else {
    pullPendingStateUpdatesLocked();
    mHistoryCur.batteryLevel=(byte)level;
    mHistoryCur.states|=HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
    if (DEBUG_HISTORY)     Slog.v(TAG,"String_Node_Str" + Integer.toHexString(mHistoryCur.states));
    addHistoryRecordLocked(mSecRealtime,mSecUptime);
    mDischargeCurrentLevel=mDischargePlugLevel=level;
    if (level < mDischargeUnplugLevel) {
      mLowDischargeAmountSinceCharge+=mDischargeUnplugLevel - level - 1;
      mHighDischargeAmountSinceCharge+=mDischargeUnplugLevel - level;
    }
    updateDischargeScreenLevelsLocked(screenOn,screenOn);
    updateTimeBasesLocked(false,!screenOn,uptime,realtime);
    mNumChargeStepDurations=0;
    mLastChargeStepLevel=level;
    mMaxChargeStepLevel=level;
    mLastChargeStepTime=-1;
    mInitStepMode=mCurStepMode;
    mModStepMode=0;
  }
  if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
    if (mFile != null) {
      writeAsyncLocked();
    }
  }
}

Fixed version (ground truth):
void setOnBatteryLocked(final long mSecRealtime,final long mSecUptime,final boolean onBattery,final int oldStatus,final int level){
  boolean doWrite=false;
  Message m=mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
  m.arg1=onBattery ? 1 : 0;
  mHandler.sendMessage(m);
  final long uptime=mSecUptime * 1000;
  final long realtime=mSecRealtime * 1000;
  final boolean screenOn=mScreenState == Display.STATE_ON;
  if (onBattery) {
    boolean reset=false;
    if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL || level >= 90 || (mDischargeCurrentLevel < 20 && level >= 80) || (getHighDischargeAmountSinceCharge() >= 200 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
      Slog.i(TAG,"String_Node_Str" + level + "String_Node_Str"+ oldStatus+ "String_Node_Str"+ mDischargeCurrentLevel+ "String_Node_Str"+ getLowDischargeAmountSinceCharge()+ "String_Node_Str"+ getHighDischargeAmountSinceCharge());
      if (getLowDischargeAmountSinceCharge() >= 20) {
        final Parcel parcel=Parcel.obtain();
        writeSummaryToParcel(parcel,true);
        BackgroundThread.getHandler().post(new Runnable(){
          @Override public void run(){
synchronized (mCheckinFile) {
              FileOutputStream stream=null;
              try {
                stream=mCheckinFile.startWrite();
                stream.write(parcel.marshall());
                stream.flush();
                FileUtils.sync(stream);
                stream.close();
                mCheckinFile.finishWrite(stream);
              }
 catch (              IOException e) {
                Slog.w("String_Node_Str","String_Node_Str",e);
                mCheckinFile.failWrite(stream);
              }
 finally {
                parcel.recycle();
              }
            }
          }
        }
);
      }
      doWrite=true;
      resetAllStatsLocked();
      mDischargeStartLevel=level;
      reset=true;
      mNumDischargeStepDurations=0;
    }
    mOnBattery=mOnBatteryInternal=onBattery;
    mLastDischargeStepLevel=level;
    mMinDischargeStepLevel=level;
    mLastDischargeStepTime=-1;
    mInitStepMode=mCurStepMode;
    mModStepMode=0;
    pullPendingStateUpdatesLocked();
    mHistoryCur.batteryLevel=(byte)level;
    mHistoryCur.states&=~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
    if (DEBUG_HISTORY)     Slog.v(TAG,"String_Node_Str" + Integer.toHexString(mHistoryCur.states));
    if (reset) {
      mRecordingHistory=true;
      startRecordingHistory(mSecRealtime,mSecUptime,reset);
    }
    addHistoryRecordLocked(mSecRealtime,mSecUptime);
    mDischargeCurrentLevel=mDischargeUnplugLevel=level;
    if (screenOn) {
      mDischargeScreenOnUnplugLevel=level;
      mDischargeScreenOffUnplugLevel=0;
    }
 else {
      mDischargeScreenOnUnplugLevel=0;
      mDischargeScreenOffUnplugLevel=level;
    }
    mDischargeAmountScreenOn=0;
    mDischargeAmountScreenOff=0;
    updateTimeBasesLocked(true,!screenOn,uptime,realtime);
  }
 else {
    mOnBattery=mOnBatteryInternal=onBattery;
    pullPendingStateUpdatesLocked();
    mHistoryCur.batteryLevel=(byte)level;
    mHistoryCur.states|=HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
    if (DEBUG_HISTORY)     Slog.v(TAG,"String_Node_Str" + Integer.toHexString(mHistoryCur.states));
    addHistoryRecordLocked(mSecRealtime,mSecUptime);
    mDischargeCurrentLevel=mDischargePlugLevel=level;
    if (level < mDischargeUnplugLevel) {
      mLowDischargeAmountSinceCharge+=mDischargeUnplugLevel - level - 1;
      mHighDischargeAmountSinceCharge+=mDischargeUnplugLevel - level;
    }
    updateDischargeScreenLevelsLocked(screenOn,screenOn);
    updateTimeBasesLocked(false,!screenOn,uptime,realtime);
    mNumChargeStepDurations=0;
    mLastChargeStepLevel=level;
    mMaxChargeStepLevel=level;
    mLastChargeStepTime=-1;
    mInitStepMode=mCurStepMode;
    mModStepMode=0;
  }
  if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
    if (mFile != null) {
      writeAsyncLocked();
    }
  }
}