From c9edd1b28ae429e6c34462917fcb5c9d616e0ef8 Mon Sep 17 00:00:00 2001
From: antirez <antirez@gmail.com>
Date: Fri, 13 Apr 2012 12:13:02 +0200
Subject: [PATCH] Globals protection global() function modified for speed and
 correctness.

---
 src/scripting.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/scripting.c b/src/scripting.c
index 138437cf..44ceb1e2 100644
--- a/src/scripting.c
+++ b/src/scripting.c
@@ -427,7 +427,7 @@ void scriptingEnableGlobalsProtection(lua_State *lua) {
 
     /* strict.lua from: http://metalua.luaforge.net/src/lib/strict.lua.html.
      * Modified to be adapted to Redis. */
-    s[j++]="mt = {}\n";
+    s[j++]="local mt = {}\n";
     s[j++]="setmetatable(_G, mt)\n";
     s[j++]="mt.declared = {}\n";
     s[j++]="mt.__newindex = function (t, n, v)\n";
@@ -447,7 +447,11 @@ void scriptingEnableGlobalsProtection(lua_State *lua) {
     s[j++]="  return rawget(t, n)\n";
     s[j++]="end\n";
     s[j++]="function global(...)\n";
-    s[j++]="   for _, v in ipairs{...} do mt.declared[v] = true end\n";
+    s[j++]="  local nargs = select(\"#\",...)\n";
+    s[j++]="  for i = 1, nargs do\n";
+    s[j++]="    local v = select(i,...)\n";
+    s[j++]="    mt.declared[v] = true\n";
+    s[j++]="  end\n";
     s[j++]="end\n";
     s[j++]=NULL;