From 9ed12b0f07edb342aaff21c130d325fd158a9d5b Mon Sep 17 00:00:00 2001 From: rubidium Date: Fri, 25 Apr 2014 15:40:32 +0000 Subject: (svn r26509) -Codechange: replace strdup with stredup (the latter ensures the return is not NULL) --- src/script/api/script_controller.cpp | 2 +- src/script/api/script_error.cpp | 3 ++- src/script/api/script_event_types.cpp | 12 ++++++++++++ src/script/api/script_event_types.hpp | 11 ++--------- src/script/api/script_log.cpp | 3 ++- src/script/api/script_object.cpp | 2 +- src/script/api/script_text.cpp | 12 +++++++++++- src/script/api/script_text.hpp | 5 ++--- src/script/script_config.cpp | 10 +++++----- src/script/script_info.cpp | 12 ++++++------ src/script/script_scanner.cpp | 8 ++++---- src/script/squirrel.cpp | 2 +- src/script/squirrel_helper.hpp | 4 ++-- src/script/squirrel_std.cpp | 4 +++- 14 files changed, 54 insertions(+), 36 deletions(-) (limited to 'src/script') diff --git a/src/script/api/script_controller.cpp b/src/script/api/script_controller.cpp index 4c3a5cd6e..65e542c02 100644 --- a/src/script/api/script_controller.cpp +++ b/src/script/api/script_controller.cpp @@ -154,7 +154,7 @@ ScriptController::~ScriptController() sq_newslot(vm, -3, SQFalse); sq_pop(vm, 1); - controller->loaded_library[strdup(library_name)] = strdup(fake_class); + controller->loaded_library[stredup(library_name)] = stredup(fake_class); } /* Find the real class inside the fake class (like 'sets.Vector') */ diff --git a/src/script/api/script_error.cpp b/src/script/api/script_error.cpp index 9dd6ece19..d30ad9c66 100644 --- a/src/script/api/script_error.cpp +++ b/src/script/api/script_error.cpp @@ -12,6 +12,7 @@ #include "../../stdafx.h" #include "script_error.hpp" #include "../../core/bitmath_func.hpp" +#include "../../string_func.h" #include "../../safeguards.h" @@ -25,7 +26,7 @@ ScriptError::ScriptErrorMapString ScriptError::error_map_string = ScriptError::S /* static */ char *ScriptError::GetLastErrorString() { - return strdup((*error_map_string.find(ScriptError::GetLastError())).second); + return stredup((*error_map_string.find(ScriptError::GetLastError())).second); } /* static */ ScriptErrorType ScriptError::StringToError(StringID internal_string_id) diff --git a/src/script/api/script_event_types.cpp b/src/script/api/script_event_types.cpp index 8c1395c11..277ce22ec 100644 --- a/src/script/api/script_event_types.cpp +++ b/src/script/api/script_event_types.cpp @@ -17,6 +17,7 @@ #include "../../settings_type.h" #include "../../engine_base.h" #include "../../articulated_vehicles.h" +#include "../../string_func.h" #include "table/strings.h" #include "../../safeguards.h" @@ -119,6 +120,17 @@ bool ScriptEventCompanyAskMerger::AcceptMerger() return ScriptObject::DoCommand(0, this->owner, 0, CMD_BUY_COMPANY); } +ScriptEventAdminPort::ScriptEventAdminPort(const char *json) : + ScriptEvent(ET_ADMIN_PORT), + json(stredup(json)) +{ +} + +ScriptEventAdminPort::~ScriptEventAdminPort() +{ + free(this->json); +} + #define SKIP_EMPTY(p) while (*(p) == ' ' || *(p) == '\n' || *(p) == '\r') (p)++; #define RETURN_ERROR(stack) { ScriptLog::Error("Received invalid JSON data from AdminPort."); if (stack != 0) sq_pop(vm, stack); return NULL; } diff --git a/src/script/api/script_event_types.hpp b/src/script/api/script_event_types.hpp index 62834113d..943016897 100644 --- a/src/script/api/script_event_types.hpp +++ b/src/script/api/script_event_types.hpp @@ -839,15 +839,8 @@ public: /** * @param json The JSON string which got sent. */ - ScriptEventAdminPort(const char *json) : - ScriptEvent(ET_ADMIN_PORT), - json(strdup(json)) - {} - - ~ScriptEventAdminPort() - { - free(this->json); - } + ScriptEventAdminPort(const char *json); + ~ScriptEventAdminPort(); /** * Convert an ScriptEvent to the real instance. diff --git a/src/script/api/script_log.cpp b/src/script/api/script_log.cpp index 6e725617c..d9fbbde98 100644 --- a/src/script/api/script_log.cpp +++ b/src/script/api/script_log.cpp @@ -14,6 +14,7 @@ #include "../../core/alloc_func.hpp" #include "../../debug.h" #include "../../window_func.h" +#include "../../string_func.h" #include "../../safeguards.h" @@ -53,7 +54,7 @@ /* Free last message, and write new message */ free(log->lines[log->pos]); - log->lines[log->pos] = strdup(message); + log->lines[log->pos] = stredup(message); log->type[log->pos] = level; /* Cut string after first \n */ diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp index 47de2b1a7..49dba6bb7 100644 --- a/src/script/api/script_object.cpp +++ b/src/script/api/script_object.cpp @@ -264,7 +264,7 @@ ScriptObject::ActiveInstance::~ActiveInstance() char buffer[64]; ::GetString(buffer, string, lastof(buffer)); ::str_validate(buffer, lastof(buffer), SVS_NONE); - return ::strdup(buffer); + return ::stredup(buffer); } /* static */ void ScriptObject::SetCallbackVariable(int index, int value) diff --git a/src/script/api/script_text.cpp b/src/script/api/script_text.cpp index 9fb297990..1529d4577 100644 --- a/src/script/api/script_text.cpp +++ b/src/script/api/script_text.cpp @@ -19,6 +19,16 @@ #include "../../safeguards.h" +RawText::RawText(const char *text) : text(stredup(text)) +{ +} + +RawText::~RawText() +{ + free(this->text); +} + + ScriptText::ScriptText(HSQUIRRELVM vm) : ZeroedMemoryAllocator() { @@ -73,7 +83,7 @@ SQInteger ScriptText::_SetParam(int parameter, HSQUIRRELVM vm) const SQChar *value; sq_getstring(vm, -1, &value); - this->params[parameter] = strdup(SQ2OTTD(value)); + this->params[parameter] = stredup(SQ2OTTD(value)); ValidateString(this->params[parameter]); break; } diff --git a/src/script/api/script_text.hpp b/src/script/api/script_text.hpp index 9b75e21c5..fe38be197 100644 --- a/src/script/api/script_text.hpp +++ b/src/script/api/script_text.hpp @@ -42,9 +42,8 @@ public: */ class RawText : public Text { public: - RawText(const char *text) : - text(strdup(text)) {} - ~RawText() { free(this->text); } + RawText(const char *text); + ~RawText(); /* virtual */ const char *GetEncodedText() { return this->text; } private: diff --git a/src/script/script_config.cpp b/src/script/script_config.cpp index 768ff5e21..a6e41476e 100644 --- a/src/script/script_config.cpp +++ b/src/script/script_config.cpp @@ -21,7 +21,7 @@ void ScriptConfig::Change(const char *name, int version, bool force_exact_match, bool is_random) { free(this->name); - this->name = (name == NULL) ? NULL : strdup(name); + this->name = (name == NULL) ? NULL : stredup(name); this->info = (name == NULL) ? NULL : this->FindInfo(this->name, version, force_exact_match); this->version = (info == NULL) ? -1 : info->GetVersion(); this->is_random = is_random; @@ -45,14 +45,14 @@ void ScriptConfig::Change(const char *name, int version, bool force_exact_match, ScriptConfig::ScriptConfig(const ScriptConfig *config) { - this->name = (config->name == NULL) ? NULL : strdup(config->name); + this->name = (config->name == NULL) ? NULL : stredup(config->name); this->info = config->info; this->version = config->version; this->config_list = NULL; this->is_random = config->is_random; for (SettingValueList::const_iterator it = config->settings.begin(); it != config->settings.end(); it++) { - this->settings[strdup((*it).first)] = (*it).second; + this->settings[stredup((*it).first)] = (*it).second; } this->AddRandomDeviation(); } @@ -117,7 +117,7 @@ void ScriptConfig::SetSetting(const char *name, int value) if (it != this->settings.end()) { (*it).second = value; } else { - this->settings[strdup(name)] = value; + this->settings[stredup(name)] = value; } } @@ -160,7 +160,7 @@ int ScriptConfig::GetVersion() const void ScriptConfig::StringToSettings(const char *value) { - char *value_copy = strdup(value); + char *value_copy = stredup(value); char *s = value_copy; while (s != NULL) { diff --git a/src/script/script_info.cpp b/src/script/script_info.cpp index 27bcec1eb..83d3bab8a 100644 --- a/src/script/script_info.cpp +++ b/src/script/script_info.cpp @@ -83,9 +83,9 @@ bool ScriptInfo::CheckMethod(const char *name) const } /* Get location information of the scanner */ - info->main_script = strdup(info->scanner->GetMainScript()); + info->main_script = stredup(info->scanner->GetMainScript()); const char *tar_name = info->scanner->GetTarFile(); - if (tar_name != NULL) info->tar_file = strdup(tar_name); + if (tar_name != NULL) info->tar_file = stredup(tar_name); /* Cache the data the info file gives us. */ if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAuthor", &info->author, MAX_GET_OPS)) return SQ_ERROR; @@ -133,7 +133,7 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm) if (strcmp(key, "name") == 0) { const SQChar *sqvalue; if (SQ_FAILED(sq_getstring(vm, -1, &sqvalue))) return SQ_ERROR; - char *name = strdup(SQ2OTTD(sqvalue)); + char *name = stredup(SQ2OTTD(sqvalue)); char *s; ValidateString(name); @@ -146,7 +146,7 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm) } else if (strcmp(key, "description") == 0) { const SQChar *sqdescription; if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR; - config.description = strdup(SQ2OTTD(sqdescription)); + config.description = stredup(SQ2OTTD(sqdescription)); ValidateString(config.description); items |= 0x002; } else if (strcmp(key, "min_value") == 0) { @@ -264,8 +264,8 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm) const char *label = SQ2OTTD(sq_label); ValidateString(label); - /* !Contains() prevents strdup from leaking. */ - if (!config->labels->Contains(key)) config->labels->Insert(key, strdup(label)); + /* !Contains() prevents stredup from leaking. */ + if (!config->labels->Contains(key)) config->labels->Insert(key, stredup(label)); sq_pop(vm, 2); } diff --git a/src/script/script_scanner.cpp b/src/script/script_scanner.cpp index 3796aafd5..98bc1f385 100644 --- a/src/script/script_scanner.cpp +++ b/src/script/script_scanner.cpp @@ -29,12 +29,12 @@ bool ScriptScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename) { free(this->main_script); - this->main_script = strdup(filename); + this->main_script = stredup(filename); if (this->main_script == NULL) return false; free(this->tar_file); if (tar_filename != NULL) { - this->tar_file = strdup(tar_filename); + this->tar_file = stredup(tar_filename); if (this->tar_file == NULL) return false; } else { this->tar_file = NULL; @@ -150,13 +150,13 @@ void ScriptScanner::RegisterScript(ScriptInfo *info) return; } - this->info_list[strdup(script_name)] = info; + this->info_list[stredup(script_name)] = info; if (!info->IsDeveloperOnly() || _settings_client.gui.ai_developer_tools) { /* Add the script to the 'unique' script list, where only the highest version * of the script is registered. */ if (this->info_single_list.find(script_original_name) == this->info_single_list.end()) { - this->info_single_list[strdup(script_original_name)] = info; + this->info_single_list[stredup(script_original_name)] = info; } else if (this->info_single_list[script_original_name]->GetVersion() < info->GetVersion()) { this->info_single_list[script_original_name] = info; } diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp index 99e5ae1c0..aa25166b7 100644 --- a/src/script/squirrel.cpp +++ b/src/script/squirrel.cpp @@ -260,7 +260,7 @@ bool Squirrel::CallStringMethodStrdup(HSQOBJECT instance, const char *method_nam HSQOBJECT ret; if (!this->CallMethod(instance, method_name, &ret, suspend)) return false; if (ret._type != OT_STRING) return false; - *res = strdup(ObjectToString(&ret)); + *res = stredup(ObjectToString(&ret)); ValidateString(*res); return true; } diff --git a/src/script/squirrel_helper.hpp b/src/script/squirrel_helper.hpp index 3e3764384..ff00712c0 100644 --- a/src/script/squirrel_helper.hpp +++ b/src/script/squirrel_helper.hpp @@ -26,7 +26,7 @@ template const char *GetClassName(); namespace SQConvert { /** * Pointers assigned to this class will be free'd when this instance - * comes out of scope. Useful to make sure you can use strdup(), + * comes out of scope. Useful to make sure you can use stredup(), * without leaking memory. */ struct SQAutoFreePointers : SmallVector { @@ -113,7 +113,7 @@ namespace SQConvert { const SQChar *tmp; sq_getstring(vm, -1, &tmp); - char *tmp_str = strdup(SQ2OTTD(tmp)); + char *tmp_str = stredup(SQ2OTTD(tmp)); sq_poptop(vm); *ptr->Append() = (void *)tmp_str; str_validate(tmp_str, tmp_str + strlen(tmp_str)); diff --git a/src/script/squirrel_std.cpp b/src/script/squirrel_std.cpp index f09471554..0fb8564c8 100644 --- a/src/script/squirrel_std.cpp +++ b/src/script/squirrel_std.cpp @@ -16,11 +16,13 @@ #include "squirrel_std.hpp" #include "../core/alloc_func.hpp" #include "../core/math_func.hpp" +#include "../string_func.h" /* Due to the different characters for Squirrel, the scstrcat might be a simple * strcat which triggers the safeguard. But it isn't always a simple strcat. */ #include "../safeguards.h" #undef strcat +#undef strdup SQInteger SquirrelStd::min(HSQUIRRELVM vm) @@ -71,7 +73,7 @@ SQInteger SquirrelStd::require(HSQUIRRELVM vm) real_filename = ReallocT(real_filename, scstrlen(real_filename) + scstrlen(filename) + 1); scstrcat(real_filename, filename); /* Tars dislike opening files with '/' on Windows.. so convert it to '\\' ;) */ - char *filen = strdup(SQ2OTTD(real_filename)); + char *filen = stredup(SQ2OTTD(real_filename)); #if (PATHSEPCHAR != '/') for (char *n = filen; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR; #endif -- cgit v1.2.3-54-g00ecf