redis如何检测lua脚本的合理性。
2014-07-28 17:33
309 查看
char *s[32]; sds code = sdsempty(); int j = 0; /* strict.lua from: http://metalua.luaforge.net/src/lib/strict.lua.html. * Modified to be adapted to Redis. */ s[j++]="local mt = {}\n"; s[j++]="setmetatable(_G, mt)\n"; s[j++]="mt.__newindex = function (t, n, v)\n"; s[j++]=" if debug.getinfo(2) then\n"; s[j++]=" local w = debug.getinfo(2, \"S\").what\n"; s[j++]=" if w ~= \"main\" and w ~= \"C\" then\n"; s[j++]=" error(\"Script attempted to create global variable '\"..tostring(n)..\"'\", 2)\n"; s[j++]=" end\n"; s[j++]=" end\n"; s[j++]=" rawset(t, n, v)\n"; s[j++]="end\n"; s[j++]="mt.__index = function (t, n)\n"; s[j++]=" if debug.getinfo(2) and debug.getinfo(2, \"S\").what ~= \"C\" then\n"; s[j++]=" error(\"Script attempted to access unexisting global variable '\"..tostring(n)..\"'\", 2)\n"; s[j++]=" end\n"; s[j++]=" return rawget(t, n)\n"; s[j++]="end\n"; s[j++]=NULL; for (j = 0; s[j] != NULL; j++) code = sdscatlen(code,s[j],strlen(s[j])); luaL_loadbuffer(lua,code,sdslen(code),"@enable_strict_lua"); lua_pcall(lua,0,0,0); sdsfree(code);
主要是两点:
1、禁止设置新的全局变量
2、禁止访问不存在的全局变量
另外并没有限制:
1、修改已存在的全局变量
redis 参考了以下代码:
http://metalua.luaforge.net/src/lib/strict.lua.html
相关文章推荐
- Redis如何使用lua脚本实例教程
- Redis进阶实践之十九 Redis如何使用lua脚本
- cocos2d-x-lua如何导出自定义类到lua脚本环境
- 【程序语言】C/C++中如何使用Lua脚本
- 如何在C++中集成Lua脚本(LuaPlus篇)
- 如何在游戏机制中使用AI/剧情脚本----基于LUA
- 如何在C++中集成Lua脚本(LuaPlus篇)
- 如何在C++中集成Lua脚本(LuaPlus篇)
- 【转贴】如何在C++中集成Lua脚本(LuaPlus篇)
- 自己写了一个perl脚本检测redis(nagios插件)
- 如何在CEGUI中使用Lua脚本入门学习
- 如何用C API遍历Lua脚本中的表
- 如何在游戏机制中使用AI/剧情脚本----基于LUA
- (如何在C++代码中调用Lua脚本的函数?)
- 【转贴】如何在C++中集成Lua脚本(LuaPlus篇)
- 如何在C++中集成Lua脚本(LuaPlus篇)
- 如何在C++中集成LUA脚本(LuaWrapper For C++篇)
- 在游戏中如何使用LUA脚本语言
- Redis 2.6 Lua脚本功能实现分析