Fix(license): IP list truncated at 4KB causing permanent data loss

This commit is contained in:
yuanyuanxiang
2026-05-25 21:04:36 +02:00
parent d6fb612475
commit 620aaf6827
2 changed files with 41 additions and 37 deletions

View File

@@ -208,9 +208,25 @@ public:
virtual std::string GetStr(const std::string& MainKey, const std::string& SubKey, const std::string& def = "")
{
char buf[4096] = { 0 }; // 增大缓冲区以支持较长的值(如 IP 列表)
DWORD n = ::GetPrivateProfileStringA(MainKey.c_str(), SubKey.c_str(), def.c_str(), buf, sizeof(buf), m_IniFilePath);
return std::string(buf);
// 动态扩容读取GetPrivateProfileStringA 在缓冲不够时会从中间截断,
// 必须以"是否返回 bufSize-1"判断截断并翻倍重读,否则长值(如团购授权的
// IP 列表)会被悄无声息地切断,且后续 read-modify-write 把截断结果写回时
// 造成永久数据丢失。
DWORD bufSize = 4096;
const DWORD kMaxBufSize = 1024 * 1024; // 1MB 兜底,避免失控
std::vector<char> buf;
for (;;) {
buf.assign(bufSize, 0);
DWORD n = ::GetPrivateProfileStringA(MainKey.c_str(), SubKey.c_str(),
def.c_str(), buf.data(), bufSize,
m_IniFilePath);
// 未截断n < bufSize - 1
if (n + 1 < bufSize || bufSize >= kMaxBufSize) {
return std::string(buf.data(), n);
}
bufSize *= 2;
if (bufSize > kMaxBufSize) bufSize = kMaxBufSize;
}
}
virtual bool SetStr(const std::string& MainKey, const std::string& SubKey, const std::string& Data)