fix(config): stop reads from creating empty registry subkeys

iniFile/binFile::GetCachedKey used RegCreateKeyExA for every access, so a plain
GetStr/GetInt on a missing section materialised an empty subkey. CScreenManager's
constructor calls LoadQualityProfiles() unconditionally, which reads profile0..
profile5 x4 keys, so every client start left six zero-valued subkeys behind:
126 of them on the dev box, all with zero values. The server's HKCU\SoftwareYAMA\quality key is the same artefact.

Split the helper by intent: writes go through RegCreateKeyExA, reads through
RegOpenKeyExA. Both still ask for KEY_READ|KEY_WRITE, because the handle cache is
shared and a read-only cached handle would make a later SetStr on the same
section fail.

Also fixes a second bug in SaveQualityProfiles: when a level matched the default
it simply continued, so a level the operator reset to default kept the old
custom values in the registry and the reset never took effect. Add
SectionExists() (open-only, never creates) and write the defaults back when the
section already exists.
This commit is contained in:
yuanyuanxiang committed 2026-11-02 14:12:21 +01:00
1 parent 454d25d37f
commit 8b92f32c47
2 files changed
+55 -17

No files matched your search

+42 -12
View File
@@ -323,8 +323,14 @@ private:
// 注册表键句柄缓存,避免频繁 RegOpenKeyEx/RegCloseKey
mutable std::map<std::string, HKEY> m_keyCache;
// 获取缓存的键句柄,如果不存在则打开并缓存
HKEY GetCachedKey(const std::string& mainKey) const
// 获取缓存的键句柄
// create=true :不存在就建(只有写入才该这么做)
// create=false :只打开、绝不创建。纯读取把一个不存在的节凭空写成空键,
// 会在注册表里留下一堆没有任何值的子键(典型受害者:客户端
// LoadQualityProfiles 无条件读 profile0~profile5)。
// 两条路径都申请 KEY_READ|KEY_WRITE:缓存里可能先存下读取时打开的句柄,
// 若只给 KEY_READ,后续对同一节的 SetStr 会因权限不足失败。
HKEY GetCachedKey(const std::string& mainKey, bool create) const
{
std::string fullPath = m_SubKeyPath + "\\" + mainKey;
@@ -334,8 +340,12 @@ private:
}
HKEY hKey = NULL;
if (RegCreateKeyExA(m_hRootKey, fullPath.c_str(), 0, NULL, 0,
KEY_READ | KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
LONG rc = create
? RegCreateKeyExA(m_hRootKey, fullPath.c_str(), 0, NULL, 0,
KEY_READ | KEY_WRITE, NULL, &hKey, NULL)
: RegOpenKeyExA(m_hRootKey, fullPath.c_str(), 0,
KEY_READ | KEY_WRITE, &hKey);
if (rc == ERROR_SUCCESS) {
m_keyCache[fullPath] = hKey;
return hKey;
}
@@ -379,7 +389,7 @@ public:
// 写入字符串(使用缓存的键句柄)
bool SetStr(const std::string& MainKey, const std::string& SubKey, const std::string& Data) override
{
HKEY hKey = GetCachedKey(MainKey);
HKEY hKey = GetCachedKey(MainKey, true);
if (!hKey)
return false;
@@ -392,7 +402,7 @@ public:
// (PEM 正文、base64 证书等)截断成空,长值一律走 ReadRegistryValueSz。
std::string GetStr(const std::string& MainKey, const std::string& SubKey, const std::string& def = "") override
{
HKEY hKey = GetCachedKey(MainKey);
HKEY hKey = GetCachedKey(MainKey, false);
if (!hKey)
return def;
@@ -427,6 +437,21 @@ public:
m_keyCache.clear();
}
// 只判断子键是否存在,绝不创建。给「配置回退到默认值、需要清掉残留」的场景用:
// 直接调 SetInt 写默认值会把不存在的节建出来,正好又变成空键。
bool SectionExists(const std::string& MainKey) const
{
std::string fullPath = m_SubKeyPath + "\\" + MainKey;
if (m_keyCache.find(fullPath) != m_keyCache.end())
return true;
HKEY hKey = NULL;
if (RegOpenKeyExA(m_hRootKey, fullPath.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
return false;
RegCloseKey(hKey);
return true;
}
// 枚举 m_SubKeyPath 下的所有子键名称
// suffix: 只返回以该后缀结尾的键名,默认为空表示返回所有键
std::vector<std::string> EnumSubKeys(const std::string& suffix = "") const
@@ -478,7 +503,7 @@ public:
{
std::vector<std::string> result;
HKEY hKey = GetCachedKey(MainKey);
HKEY hKey = GetCachedKey(MainKey, false);
if (!hKey)
return result;
@@ -521,7 +546,8 @@ private:
mutable std::map<std::string, HKEY> m_keyCache;
// 获取缓存的键句柄
HKEY GetCachedKey(const std::string& mainKey) const
// create 语义同 iniFile::GetCachedKey:读取路径不得创建子键。
HKEY GetCachedKey(const std::string& mainKey, bool create) const
{
std::string fullPath = m_SubKeyPath + "\\" + mainKey;
@@ -531,8 +557,12 @@ private:
}
HKEY hKey = NULL;
if (RegCreateKeyExA(m_hRootKey, fullPath.c_str(), 0, NULL, 0,
KEY_READ | KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
LONG rc = create
? RegCreateKeyExA(m_hRootKey, fullPath.c_str(), 0, NULL, 0,
KEY_READ | KEY_WRITE, NULL, &hKey, NULL)
: RegOpenKeyExA(m_hRootKey, fullPath.c_str(), 0,
KEY_READ | KEY_WRITE, &hKey);
if (rc == ERROR_SUCCESS) {
m_keyCache[fullPath] = hKey;
return hKey;
}
@@ -614,7 +644,7 @@ public:
private:
bool SetBinary(const std::string& MainKey, const std::string& SubKey, const BYTE* data, DWORD size)
{
HKEY hKey = GetCachedKey(MainKey);
HKEY hKey = GetCachedKey(MainKey, true);
if (!hKey)
return false;
@@ -623,7 +653,7 @@ private:
bool GetBinary(const std::string& MainKey, const std::string& SubKey, std::vector<BYTE>& outData) const
{
HKEY hKey = GetCachedKey(MainKey);
HKEY hKey = GetCachedKey(MainKey, false);
if (!hKey)
return false;