Program: 142340
Project: 142340
Buggy source:
public static void transitionLegacyUserStorage(final Context c,CommCareApp app,final byte[] oldKey,UserKeyRecord ukr) throws StorageFullException {
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + ukr.getUsername());
  try {
    final CipherPool pool=new CipherPool(){
      Object lock=new Object();
      byte[] key=oldKey;
      @Override public Cipher generateNewCipher(){
synchronized (lock) {
          try {
synchronized (key) {
              SecretKeySpec spec=new SecretKeySpec(key,"String_Node_Str");
              Cipher decrypter=Cipher.getInstance("String_Node_Str");
              decrypter.init(Cipher.DECRYPT_MODE,spec);
              return decrypter;
            }
          }
 catch (          InvalidKeyException e) {
            e.printStackTrace();
          }
catch (          NoSuchAlgorithmException e) {
            e.printStackTrace();
          }
catch (          NoSuchPaddingException e) {
            e.printStackTrace();
          }
        }
        return null;
      }
    }
;
    final android.database.sqlite.SQLiteDatabase olddb=new LegacyCommCareOpenHelper(c,new LegacyCommCareDBCursorFactory(getLegacyEncryptedModels()){
      protected CipherPool getCipherPool() throws SessionUnavailableException {
        return pool;
      }
    }
).getReadableDatabase();
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    LegacyDbHelper ldbh=new LegacyDbHelper(c,pool.borrow()){
      @Override public android.database.sqlite.SQLiteDatabase getHandle(){
        return olddb;
      }
    }
;
    final String newFileSystemRoot=app.fsPath("String_Node_Str");
    final String oldRoot=getOldFileSystemRoot();
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    LegacySqlIndexedStorageUtility<User> legacyUserStorage=new LegacySqlIndexedStorageUtility<User>("String_Node_Str",User.class,ldbh);
    try {
      for (      User u : legacyUserStorage) {
      }
    }
 catch (    RuntimeException e) {
      e.printStackTrace();
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + e.getMessage() + "String_Node_Str");
      return;
    }
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    final SQLiteDatabase currentUserDatabase=new CommCareUserOpenHelper(CommCareApplication._(),ukr.getUuid()).getWritableDatabase(CommCareSessionService.getKeyVal(oldKey));
    DbHelper newDbHelper=new DbHelper(c){
      @Override public SQLiteDatabase getHandle(){
        return currentUserDatabase;
      }
    }
;
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    try {
      LegacySqlIndexedStorageUtility<ACase> legacyCases=new LegacySqlIndexedStorageUtility<ACase>(ACase.STORAGE_KEY,ACase.class,ldbh);
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + legacyCases.getNumRecords() + "String_Node_Str");
      Map m=SqlIndexedStorageUtility.cleanCopy(legacyCases,new SqlIndexedStorageUtility<ACase>(ACase.STORAGE_KEY,ACase.class,newDbHelper));
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + m.size() + "String_Node_Str");
      SqlIndexedStorageUtility.cleanCopy(legacyUserStorage,new SqlIndexedStorageUtility<User>("String_Node_Str",User.class,newDbHelper));
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      final Map<Integer,Integer> formRecordMapping=SqlIndexedStorageUtility.cleanCopy(new LegacySqlIndexedStorageUtility<FormRecord>("String_Node_Str",FormRecord.class,ldbh),new SqlIndexedStorageUtility<FormRecord>("String_Node_Str",FormRecord.class,newDbHelper),new CopyMapper<FormRecord>(){
        @Override public FormRecord transform(        FormRecord t){
          String formRecordPath;
          try {
            formRecordPath=t.getPath(c);
            String newPath=replaceOldRoot(formRecordPath,oldRoot,newFileSystemRoot);
            if (newPath != formRecordPath) {
              ContentValues cv=new ContentValues();
              cv.put(InstanceColumns.INSTANCE_FILE_PATH,newPath);
              c.getContentResolver().update(t.getInstanceURI(),cv,null,null);
            }
            return t;
          }
 catch (          FileNotFoundException e) {
            return t;
          }
        }
      }
);
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      SqlIndexedStorageUtility.cleanCopy(new LegacySqlIndexedStorageUtility<SessionStateDescriptor>("String_Node_Str",SessionStateDescriptor.class,ldbh),new SqlIndexedStorageUtility<SessionStateDescriptor>("String_Node_Str",SessionStateDescriptor.class,newDbHelper),new CopyMapper<SessionStateDescriptor>(){
        @Override public SessionStateDescriptor transform(        SessionStateDescriptor t){
          return t.reMapFormRecordId(formRecordMapping.get(t.getFormRecordId()));
        }
      }
);
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      SqlIndexedStorageUtility.cleanCopy(new LegacySqlIndexedStorageUtility<GeocodeCacheModel>(GeocodeCacheModel.STORAGE_KEY,GeocodeCacheModel.class,ldbh),new SqlIndexedStorageUtility<GeocodeCacheModel>(GeocodeCacheModel.STORAGE_KEY,GeocodeCacheModel.class,newDbHelper));
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      SqlIndexedStorageUtility.cleanCopy(new LegacySqlIndexedStorageUtility<AndroidLogEntry>(AndroidLogEntry.STORAGE_KEY,AndroidLogEntry.class,ldbh),new SqlIndexedStorageUtility<AndroidLogEntry>(AndroidLogEntry.STORAGE_KEY,AndroidLogEntry.class,newDbHelper));
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      SqlIndexedStorageUtility.cleanCopy(new LegacySqlIndexedStorageUtility<DeviceReportRecord>("String_Node_Str",DeviceReportRecord.class,ldbh),new SqlIndexedStorageUtility<DeviceReportRecord>("String_Node_Str",DeviceReportRecord.class,newDbHelper),new CopyMapper<DeviceReportRecord>(){
        @Override public DeviceReportRecord transform(        DeviceReportRecord t){
          return new DeviceReportRecord(replaceOldRoot(t.getFilePath(),oldRoot,newFileSystemRoot),t.getKey());
        }
      }
);
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      SqlIndexedStorageUtility.cleanCopy(new LegacySqlIndexedStorageUtility<FormInstance>("String_Node_Str",FormInstance.class,ldbh),new SqlIndexedStorageUtility<FormInstance>("String_Node_Str",FormInstance.class,newDbHelper));
    }
 catch (    StorageFullException sfe) {
      throw new RuntimeException(sfe);
    }
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    ukr.setType(UserKeyRecord.TYPE_NORMAL);
    app.getStorage(UserKeyRecord.class).write(ukr);
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    new LegacySqlIndexedStorageUtility<DeviceReportRecord>("String_Node_Str",DeviceReportRecord.class,ldbh).removeAll();
    new LegacySqlIndexedStorageUtility<FormRecord>("String_Node_Str",FormRecord.class,ldbh).removeAll();
    new LegacySqlIndexedStorageUtility<SessionStateDescriptor>("String_Node_Str",SessionStateDescriptor.class,ldbh).removeAll();
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    olddb.close();
    currentUserDatabase.close();
  }
  finally {
  }
}

Fixed version (ground truth):
public static void transitionLegacyUserStorage(final Context c,CommCareApp app,final byte[] oldKey,UserKeyRecord ukr) throws StorageFullException {
  Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + ukr.getUsername());
  try {
    final CipherPool pool=new CipherPool(){
      Object lock=new Object();
      byte[] key=oldKey;
      @Override public Cipher generateNewCipher(){
synchronized (lock) {
          try {
synchronized (key) {
              SecretKeySpec spec=new SecretKeySpec(key,"String_Node_Str");
              Cipher decrypter=Cipher.getInstance("String_Node_Str");
              decrypter.init(Cipher.DECRYPT_MODE,spec);
              return decrypter;
            }
          }
 catch (          InvalidKeyException e) {
            e.printStackTrace();
          }
catch (          NoSuchAlgorithmException e) {
            e.printStackTrace();
          }
catch (          NoSuchPaddingException e) {
            e.printStackTrace();
          }
        }
        return null;
      }
    }
;
    final android.database.sqlite.SQLiteDatabase olddb=new LegacyCommCareOpenHelper(c,new LegacyCommCareDBCursorFactory(getLegacyEncryptedModels()){
      protected CipherPool getCipherPool() throws SessionUnavailableException {
        return pool;
      }
    }
).getReadableDatabase();
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    LegacyDbHelper ldbh=new LegacyDbHelper(c,pool.borrow()){
      @Override public android.database.sqlite.SQLiteDatabase getHandle(){
        return olddb;
      }
    }
;
    final String newFileSystemRoot=app.fsPath("String_Node_Str");
    final String oldRoot=getOldFileSystemRoot();
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    LegacySqlIndexedStorageUtility<User> legacyUserStorage=new LegacySqlIndexedStorageUtility<User>("String_Node_Str",User.class,ldbh);
    try {
      for (      User u : legacyUserStorage) {
      }
    }
 catch (    RuntimeException e) {
      e.printStackTrace();
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + e.getMessage() + "String_Node_Str");
      return;
    }
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    final SQLiteDatabase currentUserDatabase=new CommCareUserOpenHelper(CommCareApplication._(),ukr.getUuid()).getWritableDatabase(CommCareSessionService.getKeyVal(oldKey));
    DbHelper newDbHelper=new DbHelper(c){
      @Override public SQLiteDatabase getHandle(){
        return currentUserDatabase;
      }
    }
;
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    try {
      LegacySqlIndexedStorageUtility<ACase> legacyCases=new LegacySqlIndexedStorageUtility<ACase>(ACase.STORAGE_KEY,ACase.class,ldbh);
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + legacyCases.getNumRecords() + "String_Node_Str");
      Map m=SqlIndexedStorageUtility.cleanCopy(legacyCases,new SqlIndexedStorageUtility<ACase>(ACase.STORAGE_KEY,ACase.class,newDbHelper));
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str" + m.size() + "String_Node_Str");
      SqlIndexedStorageUtility.cleanCopy(legacyUserStorage,new SqlIndexedStorageUtility<User>("String_Node_Str",User.class,newDbHelper));
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      final Map<Integer,Integer> formRecordMapping=SqlIndexedStorageUtility.cleanCopy(new LegacySqlIndexedStorageUtility<FormRecord>("String_Node_Str",FormRecord.class,ldbh),new SqlIndexedStorageUtility<FormRecord>("String_Node_Str",FormRecord.class,newDbHelper),new CopyMapper<FormRecord>(){
        @Override public FormRecord transform(        FormRecord t){
          String formRecordPath;
          try {
            formRecordPath=t.getPath(c);
            String newPath=replaceOldRoot(formRecordPath,oldRoot,newFileSystemRoot);
            if (newPath != formRecordPath) {
              ContentValues cv=new ContentValues();
              cv.put(InstanceColumns.INSTANCE_FILE_PATH,newPath);
              c.getContentResolver().update(t.getInstanceURI(),cv,null,null);
            }
            return t;
          }
 catch (          FileNotFoundException e) {
            return t;
          }
        }
      }
);
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      SqlIndexedStorageUtility.cleanCopy(new LegacySqlIndexedStorageUtility<SessionStateDescriptor>("String_Node_Str",SessionStateDescriptor.class,ldbh),new SqlIndexedStorageUtility<SessionStateDescriptor>("String_Node_Str",SessionStateDescriptor.class,newDbHelper),new CopyMapper<SessionStateDescriptor>(){
        @Override public SessionStateDescriptor transform(        SessionStateDescriptor t){
          return t.reMapFormRecordId(formRecordMapping.get(t.getFormRecordId()));
        }
      }
);
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      SqlIndexedStorageUtility.cleanCopy(new LegacySqlIndexedStorageUtility<GeocodeCacheModel>(GeocodeCacheModel.STORAGE_KEY,GeocodeCacheModel.class,ldbh),new SqlIndexedStorageUtility<GeocodeCacheModel>(GeocodeCacheModel.STORAGE_KEY,GeocodeCacheModel.class,newDbHelper));
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      SqlIndexedStorageUtility.cleanCopy(new LegacySqlIndexedStorageUtility<DeviceReportRecord>("String_Node_Str",DeviceReportRecord.class,ldbh),new SqlIndexedStorageUtility<DeviceReportRecord>("String_Node_Str",DeviceReportRecord.class,newDbHelper),new CopyMapper<DeviceReportRecord>(){
        @Override public DeviceReportRecord transform(        DeviceReportRecord t){
          return new DeviceReportRecord(replaceOldRoot(t.getFilePath(),oldRoot,newFileSystemRoot),t.getKey());
        }
      }
);
      Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
      SqlIndexedStorageUtility.cleanCopy(new LegacySqlIndexedStorageUtility<FormInstance>("String_Node_Str",FormInstance.class,ldbh),new SqlIndexedStorageUtility<FormInstance>("String_Node_Str",FormInstance.class,newDbHelper));
    }
 catch (    StorageFullException sfe) {
      throw new RuntimeException(sfe);
    }
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    ukr.setType(UserKeyRecord.TYPE_NORMAL);
    app.getStorage(UserKeyRecord.class).write(ukr);
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    new LegacySqlIndexedStorageUtility<DeviceReportRecord>("String_Node_Str",DeviceReportRecord.class,ldbh).removeAll();
    new LegacySqlIndexedStorageUtility<FormRecord>("String_Node_Str",FormRecord.class,ldbh).removeAll();
    new LegacySqlIndexedStorageUtility<SessionStateDescriptor>("String_Node_Str",SessionStateDescriptor.class,ldbh).removeAll();
    Logger.log(AndroidLogger.TYPE_MAINTENANCE,"String_Node_Str");
    olddb.close();
    currentUserDatabase.close();
  }
  finally {
  }
}