From 159f1dc5a1602082bfb62fb461d4b38399d5d2db Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 21 Aug 2023 02:54:54 +0700 Subject: [PATCH] Fix storing user groups in databases not working Legacy(?) code that deallocated database instance before returning it and allowing to use `MutableText` as a key for database. --- sources/Data/Database/DBAPI.uc | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/sources/Data/Database/DBAPI.uc b/sources/Data/Database/DBAPI.uc index b12aaa8..6af155b 100644 --- a/sources/Data/Database/DBAPI.uc +++ b/sources/Data/Database/DBAPI.uc @@ -250,48 +250,49 @@ public final function LocalDatabaseInstance LoadLocal(BaseText databaseName) local DBRecord rootRecord; local Text rootRecordName; local LocalDatabase newConfig; - local LocalDatabaseInstance newLocalDBInstance; + local LocalDatabaseInstance newLocalDBInstance, result; local Text dbKey; if (databaseName == none) { return none; } CreateLocalDBMapIfMissing(); - if (loadedLocalDatabases.HasKey(databaseName)) + dbKey = databaseName.Copy(); + if (loadedLocalDatabases.HasKey(dbKey)) { - return LocalDatabaseInstance(loadedLocalDatabases - .GetItem(databaseName)); + result = LocalDatabaseInstance(loadedLocalDatabases.GetItem(dbKey)); + _.memory.Free(dbKey); + return result; } - // No need to check `databaseName` for being valid, + // No need to check `dbKey` for being valid, // since `Load()` will just return `none` if it is not. - newConfig = class'LocalDatabase'.static.Load(databaseName); + newConfig = class'LocalDatabase'.static.Load(dbKey); if (newConfig == none) { + _.memory.Free(dbKey); return none; } if (!newConfig.HasDefinedRoot() && !newConfig.ShouldCreateIfMissing()) { + _.memory.Free(dbKey); return none; } newLocalDBInstance = LocalDatabaseInstance(_.memory.Allocate(localDBClass)); - dbKey = databaseName.Copy(); loadedLocalDatabases.SetItem(dbKey, newLocalDBInstance); dbKey.FreeSelf(); if (newConfig.HasDefinedRoot()) { rootRecordName = newConfig.GetRootName(); - rootRecord = class'DBRecord'.static - .LoadRecord(rootRecordName, databaseName); + rootRecord = class'DBRecord'.static.LoadRecord(rootRecordName, dbKey); } else { - rootRecord = class'DBRecord'.static.NewRecord(databaseName); + rootRecord = class'DBRecord'.static.NewRecord(dbKey); rootRecordName = _.text.FromString(string(rootRecord.name)); newConfig.SetRootName(rootRecordName); newConfig.Save(); } newLocalDBInstance.Initialize(newConfig, rootRecord); - _.logger.Auto(infoLocalDatabaseLoaded).Arg(databaseName.Copy()); + _.logger.Auto(infoLocalDatabaseLoaded).Arg(dbKey); _.memory.Free(rootRecordName); - _.memory.Free(newLocalDBInstance); return newLocalDBInstance; }