mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r26776) -Codechange: use safe string functions in script/squirrel*.cpp
This commit is contained in:
parent
e63ca12ab8
commit
962c1a3242
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue