Skip to content

Commit 4c4508e

Browse files
committed
ModAPI: Fix {Get,Set}SettingsString heap corruption
Prevent a memory allocation of size 0 by initializing the result string with the fallback value before adding it to the settings. Take into account null terminator '\0' for the temporary string buffer. Fixes #298.
1 parent 497b076 commit 4c4508e

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

RSDKv5/RSDK/Core/ModAPI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,9 +1219,9 @@ void RSDK::GetSettingsString(const char *id, const char *key, String *result, co
12191219

12201220
std::string v = GetModSettingsValue(id, key);
12211221
if (!v.length()) {
1222+
InitString(result, fallback, 0);
12221223
if (currentMod->id == id)
12231224
SetSettingsString(key, result);
1224-
InitString(result, fallback, 0);
12251225
return;
12261226
}
12271227
InitString(result, v.c_str(), 0);
@@ -1532,7 +1532,7 @@ void RSDK::SetSettingsInteger(const char *key, int32 val) { SetModSettingsValue(
15321532
void RSDK::SetSettingsFloat(const char *key, float val) { SetModSettingsValue(key, std::to_string(val)); }
15331533
void RSDK::SetSettingsString(const char *key, String *val)
15341534
{
1535-
char *buf = new char[val->length];
1535+
char *buf = new char[val->length + 1]; // Take into account '\0'
15361536
GetCString(buf, val);
15371537
SetModSettingsValue(key, buf);
15381538
delete[] buf;

0 commit comments

Comments
 (0)