(svn r26776) -Codechange: use safe string functions in script/squirrel*.cpp
This commit is contained in:
@@ -19,14 +19,7 @@
|
|||||||
#include <../squirrel/sqpcheader.h>
|
#include <../squirrel/sqpcheader.h>
|
||||||
#include <../squirrel/sqvm.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"
|
#include "../safeguards.h"
|
||||||
#undef snprintf
|
|
||||||
#undef vsnprintf
|
|
||||||
#undef strcat
|
|
||||||
|
|
||||||
void Squirrel::CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
|
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];
|
SQChar buf[1024];
|
||||||
|
|
||||||
va_start(arglist, s);
|
va_start(arglist, s);
|
||||||
vsnprintf(buf, lengthof(buf), s, arglist);
|
vseprintf(buf, lastof(buf), s, arglist);
|
||||||
va_end(arglist);
|
va_end(arglist);
|
||||||
|
|
||||||
/* Check if we have a custom print function */
|
/* Check if we have a custom print function */
|
||||||
@@ -107,9 +100,9 @@ void Squirrel::PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
|
|||||||
SQChar buf[1024];
|
SQChar buf[1024];
|
||||||
|
|
||||||
va_start(arglist, s);
|
va_start(arglist, s);
|
||||||
vsnprintf(buf, lengthof(buf) - 2, s, arglist);
|
vseprintf(buf, lastof(buf) - 2, s, arglist);
|
||||||
va_end(arglist);
|
va_end(arglist);
|
||||||
strcat(buf, "\n");
|
strecat(buf, "\n", lastof(buf));
|
||||||
|
|
||||||
/* Check if we have a custom print function */
|
/* Check if we have a custom print function */
|
||||||
SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
|
SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
|
||||||
|
@@ -18,12 +18,7 @@
|
|||||||
#include "../core/math_func.hpp"
|
#include "../core/math_func.hpp"
|
||||||
#include "../string_func.h"
|
#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"
|
#include "../safeguards.h"
|
||||||
#undef strcat
|
|
||||||
#undef strdup
|
|
||||||
|
|
||||||
|
|
||||||
SQInteger SquirrelStd::min(HSQUIRRELVM vm)
|
SQInteger SquirrelStd::min(HSQUIRRELVM vm)
|
||||||
@@ -50,7 +45,6 @@ SQInteger SquirrelStd::require(HSQUIRRELVM vm)
|
|||||||
{
|
{
|
||||||
SQInteger top = sq_gettop(vm);
|
SQInteger top = sq_gettop(vm);
|
||||||
const SQChar *filename;
|
const SQChar *filename;
|
||||||
SQChar *real_filename;
|
|
||||||
|
|
||||||
sq_getstring(vm, 2, &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!");
|
DEBUG(misc, 0, "[squirrel] Couldn't detect the script-name of the 'require'-caller; this should never happen!");
|
||||||
return SQ_ERROR;
|
return SQ_ERROR;
|
||||||
}
|
}
|
||||||
real_filename = strdup(si.source);
|
|
||||||
|
char path[MAX_PATH];
|
||||||
|
strecpy(path, si.source, lastof(path));
|
||||||
/* Keep the dir, remove the rest */
|
/* Keep the dir, remove the rest */
|
||||||
SQChar *s = strrchr(real_filename, PATHSEPCHAR);
|
SQChar *s = strrchr(path, PATHSEPCHAR);
|
||||||
if (s != NULL) {
|
if (s != NULL) {
|
||||||
/* Keep the PATHSEPCHAR there, remove the rest */
|
/* Keep the PATHSEPCHAR there, remove the rest */
|
||||||
s++;
|
s++;
|
||||||
*s = '\0';
|
*s = '\0';
|
||||||
}
|
}
|
||||||
/* And now we concat, so we are relative from the current script
|
strecat(path, filename, lastof(path));
|
||||||
* 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);
|
|
||||||
#if (PATHSEPCHAR != '/')
|
#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
|
#endif
|
||||||
|
|
||||||
Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);
|
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 */
|
/* Reset the top, so the stack stays correct */
|
||||||
sq_settop(vm, top);
|
sq_settop(vm, top);
|
||||||
free(real_filename);
|
|
||||||
free(filen);
|
|
||||||
|
|
||||||
return ret ? 0 : SQ_ERROR;
|
return ret ? 0 : SQ_ERROR;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user