diff mod_storage_appendmap/mod_storage_appendmap.lua @ 2431:623e23190c3e

mod_storage_appendmap: Escape Lua keywords
author Kim Alvefur <zash@zash.se>
date Mon, 02 Jan 2017 21:04:36 +0100
parents a2625a36c092
children 4548c3d685b4
line wrap: on
line diff
--- a/mod_storage_appendmap/mod_storage_appendmap.lua	Mon Jan 02 21:03:56 2017 +0100
+++ b/mod_storage_appendmap/mod_storage_appendmap.lua	Mon Jan 02 21:04:36 2017 +0100
@@ -29,6 +29,18 @@
 	return env[key];
 end
 
+local keywords = {
+	["do"] = true; ["and"] = true; ["else"] = true; ["break"] = true;
+	["if"] = true; ["end"] = true; ["goto"] = true; ["false"] = true;
+	["in"] = true; ["for"] = true; ["then"] = true; ["local"] = true;
+	["or"] = true; ["nil"] = true; ["true"] = true; ["until"] = true;
+	["elseif"] = true; ["function"] = true; ["not"] = true;
+	["repeat"] = true; ["return"] = true; ["while"] = true;
+
+	-- _ENV is not technically a keyword but we need to treat it as such
+	["_ENV"] = true;
+};
+
 function map:set_keys(user, keyvalues)
 	local keys, values = {}, {};
 	if _VERSION == "Lua 5.1" then
@@ -36,7 +48,7 @@
 	end
 	for key, value in pairs(keyvalues) do
 		module:log("debug", "user %s sets %q to %s", user, key, tostring(value))
-		if type(key) ~= "string" or not key:find("^[%a_][%w_]*$") or key == "_ENV" then
+		if type(key) ~= "string" or not key:find("^[%a_][%w_]*$") or keywords[key] then
 			key = "_ENV[" .. dump(key) .. "]";
 		end
 		table.insert(keys, key);