Lunatic: protect global environment.

git-svn-id: https://svn.eduke32.com/eduke32@3967 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-07-19 12:49:07 +00:00
parent 7860fb8c01
commit a942a8f660
2 changed files with 22 additions and 5 deletions

View file

@ -2425,9 +2425,6 @@ end
-- We need this at the end because we were previously doing just that! -- We need this at the end because we were previously doing just that!
setmetatable( setmetatable(
G_, { G_, {
__newindex = function (_1, n, _2)
error("attempt to write to undeclared variable '"..n.."'", 2)
end,
__index = function (_, n) __index = function (_, n)
error("attempt to read undeclared variable '"..n.."'", 2) error("attempt to read undeclared variable '"..n.."'", 2)
end, end,
@ -2435,11 +2432,21 @@ setmetatable(
__metatable = true, __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 -- Change the environment of the running Lua thread so that everything
-- what we've set up will be available when this chunk is left. -- 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 -- In particular, we need the globals defined after setting this chunk's
-- environment earlier. -- environment earlier.
setfenv(0, _G) setfenv(0, setmetatable({}, global_mt))
do do
-- If we're running from a savegame restoration, create the restoration -- If we're running from a savegame restoration, create the restoration

View file

@ -1,6 +1,7 @@
-- test script for ELua/Lunatic Interpreter -- test script for ELua/Lunatic Interpreter
-- error=nil -- must not affect "require" -- error=nil -- must not affect "require"
local require = require
local string = require("string") local string = require("string")
local bit = require("bit") local bit = require("bit")
local math = require("math") local math = require("math")
@ -8,6 +9,15 @@ local math = require("math")
local pcall = pcall local pcall = pcall
local DBG_ = require("_LUNATIC_DBG") 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 ===---') print('---=== ELua Test script ===---')
local function printf(fmt, ...) local function printf(fmt, ...)
@ -98,7 +108,7 @@ gameevent
local unsafe = pcall(function() string.UNSAFE=true; end) 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) --DBG_.printkv('_G in test.elua', _G)
-- direct gv array access forbidden -- direct gv array access forbidden