summaryrefslogtreecommitdiff
path: root/src/script
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2014-09-06 19:28:09 +0000
committerrubidium <rubidium@openttd.org>2014-09-06 19:28:09 +0000
commit962c1a3242e213a65078ed4217112978ddb0c613 (patch)
tree6e4f30157cab540e39ba24a04d0a6f3c29156691 /src/script
parente63ca12ab815e0cd4d5cc7fd129cde04f7c4404a (diff)
downloadopenttd-962c1a3242e213a65078ed4217112978ddb0c613.tar.xz
(svn r26776) -Codechange: use safe string functions in script/squirrel*.cpp
Diffstat (limited to 'src/script')
-rw-r--r--src/script/squirrel.cpp13
-rw-r--r--src/script/squirrel_std.cpp25
2 files changed, 10 insertions, 28 deletions
diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp
index bfffc5c3d..22e5354b6 100644
--- a/src/script/squirrel.cpp
+++ b/src/script/squirrel.cpp
@@ -19,14 +19,7 @@
#include <../squirrel/sqpcheader.h>
#include <../squirrel/sqvm.h>
-/* Due to the different characters for Squirrel, the scsnprintf might be a simple
- * snprint which triggers the safeguard. But it isn't always a simple snprintf.
- * Likewise for scvsnprintf and scstrcat.
- * TODO: use properly safe functions now that Squirrel uses chars exclusively. */
#include "../safeguards.h"
-#undef snprintf
-#undef vsnprintf
-#undef strcat
void Squirrel::CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
{
@@ -51,7 +44,7 @@ void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
SQChar buf[1024];
va_start(arglist, s);
- vsnprintf(buf, lengthof(buf), s, arglist);
+ vseprintf(buf, lastof(buf), s, arglist);
va_end(arglist);
/* Check if we have a custom print function */
@@ -107,9 +100,9 @@ void Squirrel::PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
SQChar buf[1024];
va_start(arglist, s);
- vsnprintf(buf, lengthof(buf) - 2, s, arglist);
+ vseprintf(buf, lastof(buf) - 2, s, arglist);
va_end(arglist);
- strcat(buf, "\n");
+ strecat(buf, "\n", lastof(buf));
/* Check if we have a custom print function */
SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
diff --git a/src/script/squirrel_std.cpp b/src/script/squirrel_std.cpp
index ccebe2015..78cce518d 100644
--- a/src/script/squirrel_std.cpp
+++ b/src/script/squirrel_std.cpp
@@ -18,12 +18,7 @@
#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.
- * TODO: use properly safe functions now that Squirrel uses chars exclusively. */
#include "../safeguards.h"
-#undef strcat
-#undef strdup
SQInteger SquirrelStd::min(HSQUIRRELVM vm)
@@ -50,7 +45,6 @@ SQInteger SquirrelStd::require(HSQUIRRELVM vm)
{
SQInteger top = sq_gettop(vm);
const SQChar *filename;
- SQChar *real_filename;
sq_getstring(vm, 2, &filename);
@@ -61,31 +55,26 @@ SQInteger SquirrelStd::require(HSQUIRRELVM vm)
DEBUG(misc, 0, "[squirrel] Couldn't detect the script-name of the 'require'-caller; this should never happen!");
return SQ_ERROR;
}
- real_filename = strdup(si.source);
+
+ char path[MAX_PATH];
+ strecpy(path, si.source, lastof(path));
/* Keep the dir, remove the rest */
- SQChar *s = strrchr(real_filename, PATHSEPCHAR);
+ SQChar *s = strrchr(path, PATHSEPCHAR);
if (s != NULL) {
/* Keep the PATHSEPCHAR there, remove the rest */
s++;
*s = '\0';
}
- /* And now we concat, so we are relative from the current script
- * First, we have to make sure we have enough space for the full path */
- real_filename = ReallocT(real_filename, strlen(real_filename) + strlen(filename) + 1);
- strcat(real_filename, filename);
- /* Tars dislike opening files with '/' on Windows.. so convert it to '\\' ;) */
- char *filen = stredup(real_filename);
+ strecat(path, filename, lastof(path));
#if (PATHSEPCHAR != '/')
- for (char *n = filen; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR;
+ for (char *n = path; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR;
#endif
Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);
- bool ret = engine->LoadScript(vm, filen);
+ bool ret = engine->LoadScript(vm, path);
/* Reset the top, so the stack stays correct */
sq_settop(vm, top);
- free(real_filename);
- free(filen);
return ret ? 0 : SQ_ERROR;
}