@@ -108,13 +108,13 @@ void IConsolePrint(TextColour colour_cod
}
/* Create a copy of the string, strip if of colours and invalid
* characters and (when applicable) assign it to the console buffer */
str = stredup(string);
str_strip_colours(str);
str_validate(str, str + strlen(str));
StrMakeValidInPlace(str);
if (_network_dedicated) {
NetworkAdminConsole("console", str);
fprintf(stdout, "%s%s\n", GetLogPrefix(), str);
fflush(stdout);
IConsoleWriteToLogFile(str);
@@ -339,13 +339,13 @@ bool FiosFileScanner::AddFile(const std:
const char *t = fios_title;
if (StrEmpty(fios_title)) {
auto ps = filename.rfind(PATHSEPCHAR);
t = filename.c_str() + (ps == std::string::npos ? 0 : ps + 1);
strecpy(fios->title, t, lastof(fios->title));
str_validate(fios->title, lastof(fios->title));
StrMakeValidInPlace(fios->title, lastof(fios->title));
return true;
/**
@@ -391,13 +391,13 @@ static void FiosGetFileList(SaveLoadOper
fios->type = FIOS_TYPE_DIR;
fios->mtime = 0;
strecpy(fios->name, d_name, lastof(fios->name));
std::string dirname = std::string(d_name) + PATHSEP;
SetDParamStr(0, dirname);
GetString(fios->title, STR_SAVELOAD_DIRECTORY, lastof(fios->title));
closedir(dir);
/* Sort the subdirs always by name, ascending, remember user-sorting order */
@@ -443,13 +443,13 @@ static void GetFileTitle(const std::stri
FILE *f = FioFOpenFile(buf, "r", subdir);
if (f == nullptr) return;
size_t read = fread(title, 1, last - title, f);
assert(title + read <= last);
title[read] = '\0';
str_validate(title, last);
StrMakeValidInPlace(title, last);
FioFCloseFile(f);
* Callback for FiosGetFileList. It tells if a file is a savegame or not.
* @param fop Purpose of collecting the list.
@@ -167,13 +167,13 @@ void LoadFromHighScore()
fread(&hs->score, sizeof(hs->score), 1, fp) != 1 ||
fseek(fp, 2, SEEK_CUR) == -1) { // XXX - placeholder for hs->title, not saved anymore; compatibility
DEBUG(misc, 1, "Highscore corrupted");
i = SP_SAVED_HIGHSCORE_END;
break;
str_validate(hs->company, lastof(hs->company), SVS_NONE);
StrMakeValidInPlace(hs->company, lastof(hs->company), SVS_NONE);
hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
fclose(fp);
@@ -19,13 +19,13 @@
* Construct a new in-memory item of an Ini file.
* @param parent the group we belong to
* @param name the name of the item
*/
IniItem::IniItem(IniGroup *parent, const std::string &name) : next(nullptr)
{
this->name = str_validate(name);
this->name = StrMakeValid(name);
*parent->last_item = this;
parent->last_item = &this->next;
/** Free everything we loaded. */
@@ -47,13 +47,13 @@ void IniItem::SetValue(const std::string
* Construct a new in-memory group of an Ini file.
* @param parent the file we belong to
* @param name the name of the group
IniGroup::IniGroup(IniLoadFile *parent, const std::string &name) : next(nullptr), type(IGT_VARIABLES), item(nullptr)
this->last_item = &this->item;
*parent->last_group = this;
parent->last_group = &this->next;
if (parent->list_group_names != nullptr) {
@@ -285,13 +285,13 @@ void IniLoadFile::LoadFromDisk(const std
*e = '\0';
/* If the value was not quoted and empty, it must be nullptr */
if (!quoted && e == t) {
item->value.reset();
} else {
item->value = str_validate(std::string(t));
item->value = StrMakeValid(std::string(t));
/* it's an orphan item */
this->ReportFileError("ini: '", buffer, "' outside of group");
@@ -994,13 +994,13 @@ struct QueryStringWindow : public Window
QueryStringWindow(StringID str, StringID caption, uint max_bytes, uint max_chars, WindowDesc *desc, Window *parent, CharSetFilter afilter, QueryStringFlags flags) :
Window(desc), editbox(max_bytes, max_chars)
char *last_of = &this->editbox.text.buf[this->editbox.text.max_bytes - 1];
GetString(this->editbox.text.buf, str, last_of);
str_validate(this->editbox.text.buf, last_of, SVS_NONE);
StrMakeValidInPlace(this->editbox.text.buf, last_of, SVS_NONE);
/* Make sure the name isn't too long for the text buffer in the number of
* characters (not bytes). max_chars also counts the '\0' characters. */
while (Utf8StringLength(this->editbox.text.buf) + 1 > this->editbox.text.max_chars) {
*Utf8PrevChar(this->editbox.text.buf + strlen(this->editbox.text.buf)) = '\0';
@@ -391,13 +391,13 @@ void Packet::Recv_string(char *buffer, s
pos++;
assert(pos <= std::numeric_limits<PacketSize>::max());
this->pos = static_cast<PacketSize>(pos);
str_validate(bufp, last, settings);
StrMakeValidInPlace(bufp, last, settings);
* Reads characters (bytes) from the packet until it finds a '\0', or reaches a
* maximum of \c length characters.
* When the '\0' has not been reached in the first \c length read characters,
@@ -420,13 +420,13 @@ std::string Packet::Recv_string(size_t l
if (length == 0) {
/* The string in the packet was longer. Read until the termination. */
while (this->Recv_uint8() != '\0') {}
return str_validate(str, settings);
return StrMakeValid(str, settings);
* Get the amount of bytes that are still available for the Transfer functions.
* @return The number of bytes that still have to be transfered.
@@ -2595,13 +2595,13 @@ static ChangeInfoResult LoadTranslationT
static std::string ReadDWordAsString(ByteReader *reader)
char output[5];
for (int i = 0; i < 4; i++) output[i] = reader->ReadByte();
output[4] = '\0';
str_validate(output, lastof(output));
StrMakeValidInPlace(output, lastof(output));
return std::string(output);
* Define properties for global variables
@@ -171,13 +171,13 @@ void ShowOSErrorBox(const char *buf, boo
int CDECL main(int argc, char *argv[])
SetRandomSeed(time(nullptr));
/* Make sure our arguments contain only valid UTF-8 characters. */
for (int i = 0; i < argc; i++) ValidateString(argv[i]);
for (int i = 0; i < argc; i++) StrMakeValidInPlace(argv[i]);
return openttd_main(argc, argv);
bool GetClipboardContents(char *buffer, const char *last)
@@ -240,13 +240,13 @@ void CocoaSetupAutoreleasePool();
void CocoaReleaseAutoreleasePool();
#endif
#ifdef WITH_COCOA
CocoaSetupAutoreleasePool();
/* This is passed if we are launched by double-clicking */
if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
argv[1] = nullptr;
@@ -426,13 +426,13 @@ int APIENTRY WinMain(HINSTANCE hInstance
/* setup random seed to something quite random */
SetRandomSeed(GetTickCount());
argc = ParseCommandLine(cmdline, argv, lengthof(argv));
openttd_main(argc, argv);
/* Restore system timer resolution. */
timeEndPeriod(1);
@@ -231,13 +231,13 @@ static inline bool CheckOldSavegameType(
temp[0] = '\0'; // if reading failed, make the name empty
return false;
bool ret = VerifyOldNameChecksum(temp, len);
temp[len - 2] = '\0'; // name is null-terminated in savegame, but it's better to be sure
str_validate(temp, last);
StrMakeValidInPlace(temp, last);
return ret;
static SavegameType DetermineOldSavegameType(FILE *f, char *title, const char *last)
@@ -969,13 +969,13 @@ static void SlString(void *ptr, size_t l
str_fix_scc_encoded((char *)ptr, (char *)ptr + len);
if ((conv & SLF_ALLOW_NEWLINE) != 0) {
settings = settings | SVS_ALLOW_NEWLINE;
str_validate((char *)ptr, (char *)ptr + len, settings);
StrMakeValidInPlace((char *)ptr, (char *)ptr + len, settings);
case SLA_PTRS: break;
case SLA_NULL: break;
default: NOT_REACHED();
@@ -1013,13 +1013,13 @@ static void SlStdString(void *ptr, VarTy
str_fix_scc_encoded(buf, buf + len);
str_validate(buf, buf + len, settings);
StrMakeValidInPlace(buf, buf + len, settings);
// Store sanitized string.
str->assign(buf);
@@ -280,13 +280,13 @@ ScriptObject::ActiveInstance::~ActiveIns
/* static */ char *ScriptObject::GetString(StringID string)
char buffer[64];
::GetString(buffer, string, lastof(buffer));
::str_validate(buffer, lastof(buffer), SVS_NONE);
::StrMakeValidInPlace(buffer, lastof(buffer), SVS_NONE);
return ::stredup(buffer);
/* static */ void ScriptObject::SetCallbackVariable(int index, int value)
if ((size_t)index >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1);
@@ -309,13 +309,13 @@ ScriptObject::ActiveInstance::~ActiveIns
if (!StrEmpty(text) && (GetCommandFlags(cmd) & CMD_STR_CTRL) == 0) {
/* The string must be valid, i.e. not contain special codes. Since some
* can be made with GSText, make sure the control codes are removed. */
::str_validate(const_cast<char *>(text), text + strlen(text), SVS_NONE);
::StrMakeValidInPlace(text, SVS_NONE);
/* Set the default callback to return a true/false result of the DoCommand */
if (callback == nullptr) callback = &ScriptInstance::DoCommandReturn;
/* Are we only interested in the estimate costs? */
@@ -79,13 +79,13 @@ SQInteger ScriptText::_SetParam(int para
switch (sq_gettype(vm, -1)) {
case OT_STRING: {
const SQChar *value;
sq_getstring(vm, -1, &value);
this->params[parameter] = stredup(value);
ValidateString(this->params[parameter]);
StrMakeValidInPlace(this->params[parameter]);
case OT_INTEGER: {
SQInteger value;
sq_getinteger(vm, -1, &value);
@@ -154,13 +154,13 @@ SQInteger ScriptText::_set(HSQUIRRELVM v
int32 k;
if (sq_gettype(vm, 2) == OT_STRING) {
const SQChar *key_string;
sq_getstring(vm, 2, &key_string);
ValidateString(key_string);
StrMakeValidInPlace(key_string);
if (strncmp(key_string, "param_", 6) != 0 || strlen(key_string) > 8) return SQ_ERROR;
k = atoi(key_string + 6);
} else if (sq_gettype(vm, 2) == OT_INTEGER) {
SQInteger key;
sq_getinteger(vm, 2, &key);
@@ -119,32 +119,32 @@ SQInteger ScriptInfo::AddSetting(HSQUIRR
/* Read the table, and find all properties we care about */
sq_pushnull(vm);
while (SQ_SUCCEEDED(sq_next(vm, -2))) {
const SQChar *key;
if (SQ_FAILED(sq_getstring(vm, -2, &key))) return SQ_ERROR;
ValidateString(key);
StrMakeValidInPlace(key);
if (strcmp(key, "name") == 0) {
const SQChar *sqvalue;
if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR;
char *name = stredup(sqvalue);
char *s;
ValidateString(name);
StrMakeValidInPlace(name);
/* Don't allow '=' and ',' in configure setting names, as we need those
* 2 chars to nicely store the settings as a string. */
while ((s = strchr(name, '=')) != nullptr) *s = '_';
while ((s = strchr(name, ',')) != nullptr) *s = '_';
config.name = name;
items |= 0x001;
} else if (strcmp(key, "description") == 0) {
const SQChar *sqdescription;
if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
config.description = stredup(sqdescription);
ValidateString(config.description);
StrMakeValidInPlace(config.description);
items |= 0x002;
} else if (strcmp(key, "min_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.min_value = res;
items |= 0x004;
@@ -223,13 +223,13 @@ SQInteger ScriptInfo::AddSetting(HSQUIRR
SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
const SQChar *setting_name;
if (SQ_FAILED(sq_getstring(vm, -2, &setting_name))) return SQ_ERROR;
ValidateString(setting_name);
StrMakeValidInPlace(setting_name);
ScriptConfigItem *config = nullptr;
for (auto &item : this->config_list) {
if (strcmp(item.name, setting_name) == 0) config = &item;
@@ -250,13 +250,13 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRE
const SQChar *label;
if (SQ_FAILED(sq_getstring(vm, -2, &key_string))) return SQ_ERROR;
if (SQ_FAILED(sq_getstring(vm, -1, &label))) return SQ_ERROR;
/* Because squirrel doesn't support identifiers starting with a digit,
* we skip the first character. */
int key = atoi(key_string + 1);
ValidateString(label);
StrMakeValidInPlace(label);
/* !Contains() prevents stredup from leaking. */
if (!config->labels->Contains(key)) config->labels->Insert(key, stredup(label));
sq_pop(vm, 2);
@@ -445,13 +445,13 @@ bool Squirrel::CallMethod(HSQOBJECT inst
bool Squirrel::CallStringMethodStrdup(HSQOBJECT instance, const char *method_name, const char **res, int suspend)
HSQOBJECT ret;
if (!this->CallMethod(instance, method_name, &ret, suspend)) return false;
if (ret._type != OT_STRING) return false;
*res = stredup(ObjectToString(&ret));
ValidateString(*res);
StrMakeValidInPlace(*res);
bool Squirrel::CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend)
@@ -113,13 +113,13 @@ namespace SQConvert {
const SQChar *tmp;
sq_getstring(vm, -1, &tmp);
char *tmp_str = stredup(tmp);
sq_poptop(vm);
ptr->push_back((void *)tmp_str);
str_validate(tmp_str, tmp_str + strlen(tmp_str));
StrMakeValidInPlace(tmp_str);
return tmp_str;
template <> inline Array *GetParam(ForceType<Array *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr)
/* Sanity check of the size. */
@@ -494,13 +494,13 @@ void StringSettingDesc::MakeValueValid(s
if (this->max_length == 0 || str.size() < this->max_length) return;
/* In case a maximum length is imposed by the setting, the length
* includes the '\0' termination for network transfer purposes.
* Also ensure the string is valid after chopping of some bytes. */
std::string stdstr(str, this->max_length - 1);
str.assign(str_validate(stdstr, SVS_NONE));
str.assign(StrMakeValid(stdstr, SVS_NONE));
* Write a string to the actual setting.
* @param object The object the setting is to be saved in.
* @param str The string to save.
@@ -183,13 +183,13 @@ void str_fix_scc_encoded(char *str, cons
*str = '\0';
template <class T>
static void str_validate(T &dst, const char *str, const char *last, StringValidationSettings settings)
static void StrMakeValidInPlace(T &dst, const char *str, const char *last, StringValidationSettings settings)
/* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
while (str <= last && *str != '\0') {
size_t len = Utf8EncodedCharLen(*str);
WChar c;
@@ -243,56 +243,57 @@ static void str_validate(T &dst, const c
/* String termination, if needed, is left to the caller of this function. */
* Scans the string for valid characters and if it finds invalid ones,
* replaces them with a question mark '?' (if not ignored)
* @param str the string to validate
* @param last the last valid character of str
* @param settings the settings for the string validation.
* Scans the string for invalid characters and replaces then with a
* question mark '?' (if not ignored).
* @param str The string to validate.
* @param last The last valid character of str.
* @param settings The settings for the string validation.
void str_validate(char *str, const char *last, StringValidationSettings settings)
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings)
char *dst = str;
str_validate(dst, str, last, settings);
StrMakeValidInPlace(dst, str, last, settings);
*dst = '\0';
* Only use this function when you are sure the string ends with a '\0';
* otherwise use StrMakeValidInPlace(str, last, settings) variant.
* @param str The string (of which you are sure ends with '\0') to validate.
std::string str_validate(const std::string &str, StringValidationSettings settings)
void StrMakeValidInPlace(const char *str, StringValidationSettings settings)
/* We know it is '\0' terminated. */
StrMakeValidInPlace(const_cast<char *>(str), str + strlen(str), settings);
std::string StrMakeValid(const std::string &str, StringValidationSettings settings)
auto buf = str.data();
auto last = buf + str.size();
std::ostringstream dst;
std::ostreambuf_iterator<char> dst_iter(dst);
str_validate(dst_iter, buf, last, settings);
StrMakeValidInPlace(dst_iter, buf, last, settings);
return dst.str();
* replaces them with a question mark '?'.
void ValidateString(const char *str)
str_validate(const_cast<char *>(str), str + strlen(str) + 1);
* Checks whether the given string is valid, i.e. contains only
* valid (printable) characters and is properly terminated.
* @param last The last character of the string, i.e. the string
* must be terminated here or earlier.
@@ -36,15 +36,15 @@ char *stredup(const char *src, const cha
int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FORMAT(3, 4) NOACCESS(2);
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap) WARN_FORMAT(3, 0) NOACCESS(2);
char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
void str_validate(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2);
[[nodiscard]] std::string str_validate(const std::string &str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
void ValidateString(const char *str);
void StrMakeValidInPlace(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK) NOACCESS(2);
[[nodiscard]] std::string StrMakeValid(const std::string &str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
void StrMakeValidInPlace(const char *str, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
void str_fix_scc_encoded(char *str, const char *last) NOACCESS(2);
void str_strip_colours(char *str);
bool strtolower(char *str);
bool strtolower(std::string &str, std::string::size_type offs = 0);
@@ -372,22 +372,22 @@ static void Xunzip(byte **bufp, size_t *
if (!this->text) return;
/* Add space for trailing \0 */
this->text = ReallocT(this->text, filesize + 1);
this->text[filesize] = '\0';
/* Replace tabs and line feeds with a space since str_validate removes those. */
/* Replace tabs and line feeds with a space since StrMakeValidInPlace removes those. */
for (char *p = this->text; *p != '\0'; p++) {
if (*p == '\t' || *p == '\r') *p = ' ';
/* Check for the byte-order-mark, and skip it if needed. */
char *p = this->text + (strncmp(u8"\ufeff", this->text, 3) == 0 ? 3 : 0);
/* Make sure the string is a valid UTF-8 sequence. */
str_validate(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
StrMakeValidInPlace(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
/* Split the string on newlines. */
int row = 0;
this->lines.emplace_back(row, p);
for (; *p != '\0'; p++) {
if (*p == '\n') {
@@ -226,13 +226,13 @@ static void DedicatedHandleKeyInput()
for (char *c = input_line; *c != '\0'; c++) {
if (*c == '\n' || *c == '\r' || c == lastof(input_line)) {
*c = '\0';
str_validate(input_line, lastof(input_line));
StrMakeValidInPlace(input_line, lastof(input_line));
IConsoleCmdExec(input_line); // execute command
void VideoDriver_Dedicated::MainLoop()
Status change: