]> git.e2factory.org Git - e2factory.git/commitdiff
Make dofile2() more strict, improving config file error detection
authorTobias Ulmer <tu@emlix.com>
Thu, 6 Feb 2014 17:04:37 +0000 (18:04 +0100)
committerTobias Ulmer <tu@emlix.com>
Wed, 16 Nov 2016 14:41:17 +0000 (15:41 +0100)
The capability to disallow global variables and reading undefined
variables in configs existed for a long time but was not enabled for
most files... Implements bz#127

Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/e2lib.lua
generic/e2option.lua
local/e2tool.lua
local/licence.lua

index 49742a5d266e96e2e5b9dbf9c17e8c501550370a..2112cff7a21c955babcfe0c43ef15b8769f41699 100644 (file)
@@ -907,27 +907,26 @@ function e2lib.get_global_config()
     end
     -- use ipairs to keep the list entries ordered
     for _,path in ipairs(cf_path) do
-        local c = {}
-        c.config = function(x)
-            c.data = x
-        end
+        local data = nil
+
         e2lib.logf(4, "reading global config file: %s", path)
         local rc = e2lib.exists(path)
         if rc then
             e2lib.logf(3, "using global config file: %s", path)
-            rc, re = e2lib.dofile2(path, c, true)
+            rc, re = e2lib.dofile2(path,
+                { config = function(x) data = x end })
             if not rc then
                 return false, re
             end
-            if not c.data then
+            if not data then
                 return false, err.new("invalid configuration")
             end
-            rc, re = verify_global_config(c.data)
+            rc, re = verify_global_config(data)
             if not rc then
                 return false, re
             end
 
-            get_global_config_cache = strict.lock(c.data)
+            get_global_config_cache = strict.lock(data)
             return get_global_config_cache
         else
             e2lib.logf(4, "global config file does not exist: %s", path)
@@ -957,20 +956,18 @@ function e2lib.read_extension_config(root)
 
     e2lib.logf(3, "reading extension file: %s", extension_config)
 
-    local c = {}
-    c.extensions = function(x)
-        c.data = x
-    end
-    rc, re = e2lib.dofile2(extension_config, c, true)
+    local data = nil
+    rc, re = e2lib.dofile2(extension_config,
+        { extensions = function(x) data = x end })
     if not rc then
         return false, e:cat(re)
     end
 
-    if not c.data then
+    if not data then
         return false, e:append("invalid extension configuration")
     end
 
-    return c.data
+    return data
 end
 
 --- Successively returns the file names in the directory.
@@ -1469,17 +1466,21 @@ end
 --- Executes Lua code loaded from path.
 --@param path Filename to load lua code from (string).
 --@param gtable Environment (table) that is used instead of the global _G.
---@param allownewdefs Boolean indicating whether new variables may be defined
---                    and undefined ones read.
+--              If gtable has no metatable, the default is to reject
+--              __index and __newindex access.
 --@return True on success, false on error.
 --@return Error object on failure.
-function e2lib.dofile2(path, gtable, allownewdefs)
+function e2lib.dofile2(path, gtable)
     local e = err.new("error loading config file: %s", path)
     local chunk, msg = loadfile(path)
     if not chunk then
         return false, e:cat(msg)
     end
 
+    --for k,v in pairs(gtable) do
+    --  e2lib.logf(1, "[%s] = %s", tostring(k), tostring(v))
+    --end
+
     local function checkread(t, k)
         local x = rawget(t, k)
         if x then
@@ -1496,7 +1497,7 @@ function e2lib.dofile2(path, gtable, allownewdefs)
             path, tostring(k), tostring(v)), 0)
     end
 
-    if not allownewdefs then
+    if not getmetatable(gtable) then
         setmetatable(gtable, { __newindex = checkwrite, __index = checkread })
     end
 
index 220ef9c51bcc41a3dd6583c4ed74eb5f560101d3..47258087c0ce9733668980013a3f886eb80a1fee 100644 (file)
@@ -238,7 +238,7 @@ local function userdefaultoptions(opts)
     end
 
     local e2rc = {}
-    local rc, e = e2lib.dofile2(file, { e2rc = function(t) e2rc = t end }, false)
+    local rc, e = e2lib.dofile2(file, { e2rc = function(t) e2rc = t end })
     if not rc then
         return false, e
     end
index f5ac6ddade2315178b2643c7a89ed13182b0464b..ebbd40bad07d254563c911a72dd16aa1729ba7e1 100644 (file)
@@ -181,7 +181,7 @@ local function load_user_config(info, path, dest, index, var)
         dest[index] = table
     end
 
-    local rc, re = e2lib.dofile2(path, { [var] = func, env = info.env, string=string }, false)
+    local rc, re = e2lib.dofile2(path, { [var] = func, env = info.env, string=string })
     if not rc then
         return false, e:cat(re)
     end
@@ -252,11 +252,11 @@ local function load_user_config2(info, path, types)
     local g = {}                       -- compose the environment for the config file
     g.env = info.env                   -- env
     g.string = string                  -- string
-    for _,type in ipairs(types) do
-        g[type] = f[type]                      -- and some config functions
+    for _,typ in ipairs(types) do
+        g[typ] = f[typ]                        -- and some config functions
     end
 
-    rc, re = e2lib.dofile2(path, g, true)
+    rc, re = e2lib.dofile2(path, g)
     if not rc then
         return false, e:cat(re)
     end
@@ -599,11 +599,12 @@ local function load_env_config(info, file)
 
     table.insert(info.env_files, file)
     local path = e2lib.join(info.root, file)
-    local g = {}                  -- compose the environment for the config file
-    g.e2env = info.env                    -- env as built up so far
-    g.string = string                     -- string
-    g.env = mergeenv
-    rc, re = e2lib.dofile2(path, g, true)
+    local g = {
+        e2env = info.env,
+        string = string,
+        env = mergeenv,
+    }
+    rc, re = e2lib.dofile2(path, g)
     if not rc then
         return false, e:cat(re)
     end
index 7cebaee5b541616b4cff1d6e58726409e150b812..c3faabccff69b1a9599100ec824e79d02a14c10d 100644 (file)
@@ -166,7 +166,7 @@ function licence.load_licence_config(info)
     end
 
     rc, re = e2lib.dofile2(path,
-        { e2licence = assign, env = info.env, string = string }, false)
+        { e2licence = assign, env = info.env, string = string })
     if not rc then
         return false, re
     end