Program: 42008
Project: 42008
Buggy source:
final void updateLocked(int flags){
  long ident=Binder.clearCallingIdentity();
  try {
    String action=null;
    String oldAction=null;
    if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
      adjustStatusBarCarModeLocked();
      oldAction=UiModeManager.ACTION_EXIT_CAR_MODE;
    }
 else     if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_DESK) {
      oldAction=UiModeManager.ACTION_EXIT_DESK_MODE;
    }
    if (mCarModeEnabled) {
      if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
        adjustStatusBarCarModeLocked();
        if (oldAction != null) {
          mContext.sendBroadcast(new Intent(oldAction));
        }
        mLastBroadcastState=Intent.EXTRA_DOCK_STATE_CAR;
        action=UiModeManager.ACTION_ENTER_CAR_MODE;
      }
    }
 else     if (mDockState == Intent.EXTRA_DOCK_STATE_DESK) {
      if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_DESK) {
        if (oldAction != null) {
          mContext.sendBroadcast(new Intent(oldAction));
        }
        mLastBroadcastState=Intent.EXTRA_DOCK_STATE_DESK;
        action=UiModeManager.ACTION_ENTER_DESK_MODE;
      }
    }
 else {
      mLastBroadcastState=Intent.EXTRA_DOCK_STATE_UNDOCKED;
      action=oldAction;
    }
    if (action != null) {
      mContext.sendOrderedBroadcast(new Intent(action),null,mResultReceiver,null,Activity.RESULT_OK,null,null);
      mHoldingConfiguration=true;
    }
    if (oldAction != null && (flags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
      try {
        Intent intent=new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        updateConfigurationLocked(false);
        ActivityManagerNative.getDefault().startActivityWithConfig(null,intent,null,null,0,null,null,0,false,false,mConfiguration);
      }
 catch (      RemoteException e) {
        Slog.w(TAG,e.getCause());
      }
    }
 else {
      updateConfigurationLocked(true);
    }
    boolean keepScreenOn=mCharging && ((mCarModeEnabled && mCarModeKeepsScreenOn) || (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
    if (keepScreenOn != mWakeLock.isHeld()) {
      if (keepScreenOn) {
        mWakeLock.acquire();
      }
 else {
        mWakeLock.release();
      }
    }
  }
  finally {
    Binder.restoreCallingIdentity(ident);
  }
}

Fixed version (ground truth):
final void updateLocked(int flags){
  long ident=Binder.clearCallingIdentity();
  try {
    String action=null;
    String oldAction=null;
    if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) {
      adjustStatusBarCarModeLocked();
      oldAction=UiModeManager.ACTION_EXIT_CAR_MODE;
    }
 else     if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_DESK) {
      oldAction=UiModeManager.ACTION_EXIT_DESK_MODE;
    }
    if (mCarModeEnabled) {
      if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) {
        adjustStatusBarCarModeLocked();
        if (oldAction != null) {
          mContext.sendBroadcast(new Intent(oldAction));
        }
        mLastBroadcastState=Intent.EXTRA_DOCK_STATE_CAR;
        action=UiModeManager.ACTION_ENTER_CAR_MODE;
      }
    }
 else     if (mDockState == Intent.EXTRA_DOCK_STATE_DESK) {
      if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_DESK) {
        if (oldAction != null) {
          mContext.sendBroadcast(new Intent(oldAction));
        }
        mLastBroadcastState=Intent.EXTRA_DOCK_STATE_DESK;
        action=UiModeManager.ACTION_ENTER_DESK_MODE;
      }
    }
 else {
      mLastBroadcastState=Intent.EXTRA_DOCK_STATE_UNDOCKED;
      action=oldAction;
    }
    if (action != null) {
      mContext.sendOrderedBroadcast(new Intent(action),null,mResultReceiver,null,Activity.RESULT_OK,null,null);
      mHoldingConfiguration=true;
    }
    if (oldAction != null && (flags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
      try {
        Intent intent=new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mHoldingConfiguration=false;
        updateConfigurationLocked(false);
        ActivityManagerNative.getDefault().startActivityWithConfig(null,intent,null,null,0,null,null,0,false,false,mConfiguration);
      }
 catch (      RemoteException e) {
        Slog.w(TAG,e.getCause());
      }
    }
 else {
      updateConfigurationLocked(true);
    }
    boolean keepScreenOn=mCharging && ((mCarModeEnabled && mCarModeKeepsScreenOn) || (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn));
    if (keepScreenOn != mWakeLock.isHeld()) {
      if (keepScreenOn) {
        mWakeLock.acquire();
      }
 else {
        mWakeLock.release();
      }
    }
  }
  finally {
    Binder.restoreCallingIdentity(ident);
  }
}