History

Timeout long sandbox scripts

Zach Pomerantz committed 10 years ago

Commit e1c130d1 parent 11078821 file · +16 −1Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 1 file). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+16−1
@@ -1292,8 +1292,23 @@ void ScriptEngine::entityScriptContentAvailable(const EntityItemID& entityID, co · 1291 unmodified lines
12921292 setParentURL(scriptOrURL);
12931293 }
12941294
1295+ const int SANDBOX_TIMEOUT = 0.25 * MSECS_PER_SECOND;
12951296 QScriptEngine sandbox;
1296− QScriptValue testConstructor = sandbox.evaluate(program);
1297+ sandbox.setProcessEventsInterval(SANDBOX_TIMEOUT);
1298+ QScriptValue testConstructor;
1299+ {
1300+ QTimer timeout;
1301+ timeout.setSingleShot(true);
1302+ timeout.start(SANDBOX_TIMEOUT);
1303+ connect(&timeout, &QTimer::timeout, [&sandbox, SANDBOX_TIMEOUT]{
1304+ auto context = sandbox.currentContext();
1305+ if (context) {
1306+ // Guard against infinite loops and non-performant code
1307+ context->throwError(QString("Timed out (entity constructors are limited to %1ms)").arg(SANDBOX_TIMEOUT));
1308+ }
1309+ });
1310+ testConstructor = sandbox.evaluate(program);
1311+ }
12971312 if (hadUncaughtExceptions(sandbox, program.fileName())) {
12981313 return;
12991314 }