set: fix wrong save path / fixed null deref / more env refactor

This commit is contained in:
Hane 2025-01-07 18:53:17 +01:00
commit c87c8d0990
5 changed files with 117 additions and 119 deletions

View file

@ -27,7 +27,7 @@ namespace ini {
isCRLF = true;
*(nextLine - 1) = '\0';
} else if (nextLine) *nextLine = '\0'; // temporarily terminate the current line
log_debugcpp("curLine: " + std::string(curLine) + " ");
log_debugcpp("[SET] curLine: " + std::string(curLine) + " ");
separator = strchr(curLine, '=');
if(!separator)
@ -36,7 +36,7 @@ namespace ini {
key = trimAndAllocate(curLine);
value = trimAndAllocate(separator + 1);
values.try_emplace(key, value);
log_debugcpp("ini Map size: " + std::to_string(values.size()));
log_debugcpp("[SET] ini Map size: " + std::to_string(values.size()));
*separator = '=';
nextIteration:
@ -76,10 +76,10 @@ namespace ini {
void UserSettings::setValue(char* key, bool value, uint64_t keySize) {
char *newKey;
log_debugcpp("Pos value: " + std::to_string((intptr_t)pos));
log_debugcpp("Neg value: " + std::to_string((intptr_t)neg));
log_debugcpp("[SET] Pos value: " + std::to_string((intptr_t)pos));
log_debugcpp("[SET] Neg value: " + std::to_string((intptr_t)neg));
if (auto search = values.find(key); search != values.end()) {
log_debugcpp("Previous value: " + std::to_string((intptr_t)values[key]));
log_debugcpp("[SET] Previous value: " + std::to_string((intptr_t)values[key]));
if (!(search->second == pos || search->second == neg)) {
free(search->second);
}
@ -90,22 +90,16 @@ namespace ini {
}
newKey = (char*)calloc(keySize, sizeof(char));
memcpy(newKey, key, keySize * sizeof(char));
values.insert(std::make_pair(newKey, value ? pos : neg));
return;
}
bool UserSettings::save(const char* path) {
wchar_t maxPathBypass[] = L"\\\\?\\";
uint32_t maxPathBypassLen = (sizeof(maxPathBypass)/ sizeof(wchar_t)) - 1;
if(!path) return false;
uint64_t convertedPathSize = 0;
wchar_t* convertedPath = Utf8toUtf16(path, &convertedPathSize);
if(!convertedPath) return false;
wchar_t* utf16Path = (wchar_t*)calloc(maxPathBypassLen + convertedPathSize, sizeof(wchar_t));
memcpy(utf16Path, maxPathBypass, sizeof(wchar_t) * maxPathBypassLen);
memcpy(utf16Path + maxPathBypassLen, convertedPath, sizeof(wchar_t) * convertedPathSize);
free(convertedPath);
wchar_t* utf16Path = Utf8toUtf16(path, &convertedPathSize);
if(!utf16Path) return false;
#define releaseBeforeReturn() do { \
CloseHandle(settingsHandle); \
@ -147,7 +141,8 @@ namespace ini {
0,
CREATE_ALWAYS,
NULL);
if(settingsHandle == INVALID_HANDLE_VALUE) {
if(settingsHandle == INVALID_HANDLE_VALUE) {
log_debugcpp("[SET] Can't save to file: " + std::to_string(GetLastError()));
releaseBeforeReturn();
return false;
}
@ -197,7 +192,8 @@ namespace ini {
0,
(create ? OPEN_ALWAYS : OPEN_EXISTING),
NULL);
if(settingsHandle == INVALID_HANDLE_VALUE) {
if(settingsHandle == INVALID_HANDLE_VALUE) {
log_debugcpp("[SET] Can't create settings file: " + std::to_string(GetLastError()));
releaseBeforeReturn();
return nullptr;
}