From a942a8f6600b2f2457d4ae5a0493dee97203a7c7 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Fri, 19 Jul 2013 12:49:07 +0000 Subject: [PATCH] Lunatic: protect global environment. git-svn-id: https://svn.eduke32.com/eduke32@3967 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/lunatic/defs.ilua | 15 +++++++++++---- polymer/eduke32/source/lunatic/test.elua | 12 +++++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index f86c13090..f746d09eb 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -2425,9 +2425,6 @@ end -- We need this at the end because we were previously doing just that! setmetatable( G_, { - __newindex = function (_1, n, _2) - error("attempt to write to undeclared variable '"..n.."'", 2) - end, __index = function (_, n) error("attempt to read undeclared variable '"..n.."'", 2) end, @@ -2435,11 +2432,21 @@ setmetatable( __metatable = true, }) +local global_mt = { + __index = G_, + + __newindex = function() + error("attempt to write into the global environment") + end, + + __metatable = true, +} + -- Change the environment of the running Lua thread so that everything -- what we've set up will be available when this chunk is left. -- In particular, we need the globals defined after setting this chunk's -- environment earlier. -setfenv(0, _G) +setfenv(0, setmetatable({}, global_mt)) do -- If we're running from a savegame restoration, create the restoration diff --git a/polymer/eduke32/source/lunatic/test.elua b/polymer/eduke32/source/lunatic/test.elua index c22409b81..1b54f08e9 100644 --- a/polymer/eduke32/source/lunatic/test.elua +++ b/polymer/eduke32/source/lunatic/test.elua @@ -1,6 +1,7 @@ -- test script for ELua/Lunatic Interpreter -- error=nil -- must not affect "require" +local require = require local string = require("string") local bit = require("bit") local math = require("math") @@ -8,6 +9,15 @@ local math = require("math") local pcall = pcall local DBG_ = require("_LUNATIC_DBG") +local gv, sector, wall, sprite, spriteext = gv, sector, wall, sprite, spriteext +local actor, player, projectile = actor, player, projectile +local gameevent, gameactor = gameevent, gameactor + +local spritesofsect = spritesofsect +local hitscan = hitscan + +local assert, error, print, tostring = assert, error, print, tostring + print('---=== ELua Test script ===---') local function printf(fmt, ...) @@ -98,7 +108,7 @@ gameevent local unsafe = pcall(function() string.UNSAFE=true; end) ---tostring = nil -- REMEMBER +checkfail("tostring = nil", "attempt to write into the global environment") --DBG_.printkv('_G in test.elua', _G) -- direct gv array access forbidden