sfall-team/sfall

String truncation not working properly with new INI parser

NovaRain opened this issue · 0 comments

Taking IniConfigFolder option for example:

// in ScriptExtender.cpp
std::string ScriptExtender::iniConfigFolder;

iniConfigFolder = IniReader::GetConfigString("Scripts", "IniConfigFolder", "", 64);
size_t len = iniConfigFolder.length();
dlog_f("IniConfigFolder: %s, len: %d\n", DL_MAIN, iniConfigFolder.c_str(), len); // for testing

With IniConfigFolder=mods\thisismysuperawesomemod\withlotsofqolfeatures\youshouldreallycheckit in ddraw.ini, before the result string was truncated as it should be:

IniConfigFolder: mods\thisismysuperawesomemod\withlotsofqolfeatures\youshouldrea, len: 63

But now with new INI parser it doesn't get truncated at all:

IniConfigFolder: mods\thisismysuperawesomemod\withlotsofqolfeatures\youshouldreallycheckit, len: 73

If changing ScriptExtender::iniConfigFolder to a char array:

char ScriptExtender::iniConfigFolder[64];

size_t len = IniReader::GetConfigString("Scripts", "IniConfigFolder", "", iniConfigFolder, 64);
dlog_f("IniConfigFolder: %s, len: %d\n", DL_MAIN, iniConfigFolder.c_str(), len); // for testing

The result string is truncated correctly, but the returned size_t value is still "full length":

IniConfigFolder: mods\thisismysuperawesomemod\withlotsofqolfeatures\youshouldrea, len: 73

For GetString() variant returning size_t value, I think the mismatch can be fixed by return strlen(buf); instead of return value->size();. For the variant returning std:string, I can only think of this:

if (value->length() >= bufSize) {
	std::string truncVal(*value);
	truncVal.resize(bufSize - 1);
	return truncVal;
}