diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2012-12-03 18:18:02 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2012-12-03 18:18:02 -0200 |
commit | 65e31fb1790d8a836462fd845411f60f3adf182e (patch) | |
tree | 59fcd4924197e6497917868dfcd45e927c735394 | |
parent | f993771c70f5b0f63562259f7353d675275e8774 (diff) | |
download | lua-65e31fb1790d8a836462fd845411f60f3adf182e.tar.gz lua-65e31fb1790d8a836462fd845411f60f3adf182e.tar.bz2 lua-65e31fb1790d8a836462fd845411f60f3adf182e.zip |
Bug: load/loadfile returns wrong result when given an environment
for a binary chunk with no upvalues
-rw-r--r-- | bugs | 63 | ||||
-rw-r--r-- | lbaselib.c | 30 |
2 files changed, 74 insertions, 19 deletions
@@ -1880,8 +1880,8 @@ patch = [[ | |||
1880 | +++ lundump.c 2008/04/04 19:51:41 2.7.1.4 | 1880 | +++ lundump.c 2008/04/04 19:51:41 2.7.1.4 |
1881 | @@ -1,5 +1,5 @@ | 1881 | @@ -1,5 +1,5 @@ |
1882 | /* | 1882 | /* |
1883 | -** $Id: bugs,v 1.117 2012/09/11 12:42:14 roberto Exp roberto $ | 1883 | -** $Id: bugs,v 1.118 2012/10/01 14:05:31 roberto Exp roberto $ |
1884 | +** $Id: bugs,v 1.117 2012/09/11 12:42:14 roberto Exp roberto $ | 1884 | +** $Id: bugs,v 1.118 2012/10/01 14:05:31 roberto Exp roberto $ |
1885 | ** load precompiled Lua chunks | 1885 | ** load precompiled Lua chunks |
1886 | ** See Copyright Notice in lua.h | 1886 | ** See Copyright Notice in lua.h |
1887 | */ | 1887 | */ |
@@ -2703,7 +2703,6 @@ example = [[print(string.find(string.rep("a", 2^20), string.rep(".?", 2^20)))]], | |||
2703 | patch = [[ | 2703 | patch = [[ |
2704 | ]] | 2704 | ]] |
2705 | } | 2705 | } |
2706 | ]=] | ||
2707 | 2706 | ||
2708 | 2707 | ||
2709 | Bug{ | 2708 | Bug{ |
@@ -2797,6 +2796,64 @@ patch = [[ | |||
2797 | ]] | 2796 | ]] |
2798 | } | 2797 | } |
2799 | 2798 | ||
2799 | Bug{ | ||
2800 | what = [[load/loadfile returns wrong result when given an environment | ||
2801 | for a binary chunk with no upvalues]], | ||
2802 | report = [[Vladimir Strakh, 2012/11/28]], | ||
2803 | since = [[5.2]], | ||
2804 | fix = nil, | ||
2805 | example = [[ | ||
2806 | f = load(string.dump(function () return 1 end), nil, "b", {}) | ||
2807 | print(type(f)) --> table (whould be a function) | ||
2808 | ]], | ||
2809 | patch = [[ | ||
2810 | --- lbaselib.c 2012/04/27 14:13:19 1.274 | ||
2811 | +++ lbaselib.c 2012/12/03 20:08:15 | ||
2812 | @@ -244,5 +244,11 @@ | ||
2813 | |||
2814 | -static int load_aux (lua_State *L, int status) { | ||
2815 | - if (status == LUA_OK) | ||
2816 | +static int load_aux (lua_State *L, int status, int envidx) { | ||
2817 | + if (status == LUA_OK) { | ||
2818 | + if (envidx != 0) { /* 'env' parameter? */ | ||
2819 | + lua_pushvalue(L, envidx); /* environment for loaded function */ | ||
2820 | + if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ | ||
2821 | + lua_pop(L, 1); /* remove 'env' if not used by previous call */ | ||
2822 | + } | ||
2823 | return 1; | ||
2824 | + } | ||
2825 | else { | ||
2826 | @@ -258,9 +264,5 @@ | ||
2827 | const char *mode = luaL_optstring(L, 2, NULL); | ||
2828 | - int env = !lua_isnone(L, 3); /* 'env' parameter? */ | ||
2829 | + int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ | ||
2830 | int status = luaL_loadfilex(L, fname, mode); | ||
2831 | - if (status == LUA_OK && env) { /* 'env' parameter? */ | ||
2832 | - lua_pushvalue(L, 3); | ||
2833 | - lua_setupvalue(L, -2, 1); /* set it as 1st upvalue of loaded chunk */ | ||
2834 | - } | ||
2835 | - return load_aux(L, status); | ||
2836 | + return load_aux(L, status, env); | ||
2837 | } | ||
2838 | @@ -309,5 +311,5 @@ | ||
2839 | size_t l; | ||
2840 | - int top = lua_gettop(L); | ||
2841 | const char *s = lua_tolstring(L, 1, &l); | ||
2842 | const char *mode = luaL_optstring(L, 3, "bt"); | ||
2843 | + int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ | ||
2844 | if (s != NULL) { /* loading a string? */ | ||
2845 | @@ -322,7 +324,3 @@ | ||
2846 | } | ||
2847 | - if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */ | ||
2848 | - lua_pushvalue(L, 4); /* environment for loaded function */ | ||
2849 | - lua_setupvalue(L, -2, 1); /* set it as 1st upvalue */ | ||
2850 | - } | ||
2851 | - return load_aux(L, status); | ||
2852 | + return load_aux(L, status, env); | ||
2853 | } | ||
2854 | ]] | ||
2855 | } | ||
2856 | |||
2800 | --[=[ | 2857 | --[=[ |
2801 | Bug{ | 2858 | Bug{ |
2802 | what = [[ ]], | 2859 | what = [[ ]], |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.273 2011/11/30 13:03:24 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.274 2012/04/27 14:13:19 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -242,10 +242,16 @@ static int luaB_ipairs (lua_State *L) { | |||
242 | } | 242 | } |
243 | 243 | ||
244 | 244 | ||
245 | static int load_aux (lua_State *L, int status) { | 245 | static int load_aux (lua_State *L, int status, int envidx) { |
246 | if (status == LUA_OK) | 246 | if (status == LUA_OK) { |
247 | if (envidx != 0) { /* 'env' parameter? */ | ||
248 | lua_pushvalue(L, envidx); /* environment for loaded function */ | ||
249 | if (!lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */ | ||
250 | lua_pop(L, 1); /* remove 'env' if not used by previous call */ | ||
251 | } | ||
247 | return 1; | 252 | return 1; |
248 | else { | 253 | } |
254 | else { /* error (message is on top of the stack) */ | ||
249 | lua_pushnil(L); | 255 | lua_pushnil(L); |
250 | lua_insert(L, -2); /* put before error message */ | 256 | lua_insert(L, -2); /* put before error message */ |
251 | return 2; /* return nil plus error message */ | 257 | return 2; /* return nil plus error message */ |
@@ -256,13 +262,9 @@ static int load_aux (lua_State *L, int status) { | |||
256 | static int luaB_loadfile (lua_State *L) { | 262 | static int luaB_loadfile (lua_State *L) { |
257 | const char *fname = luaL_optstring(L, 1, NULL); | 263 | const char *fname = luaL_optstring(L, 1, NULL); |
258 | const char *mode = luaL_optstring(L, 2, NULL); | 264 | const char *mode = luaL_optstring(L, 2, NULL); |
259 | int env = !lua_isnone(L, 3); /* 'env' parameter? */ | 265 | int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ |
260 | int status = luaL_loadfilex(L, fname, mode); | 266 | int status = luaL_loadfilex(L, fname, mode); |
261 | if (status == LUA_OK && env) { /* 'env' parameter? */ | 267 | return load_aux(L, status, env); |
262 | lua_pushvalue(L, 3); | ||
263 | lua_setupvalue(L, -2, 1); /* set it as 1st upvalue of loaded chunk */ | ||
264 | } | ||
265 | return load_aux(L, status); | ||
266 | } | 268 | } |
267 | 269 | ||
268 | 270 | ||
@@ -307,9 +309,9 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { | |||
307 | static int luaB_load (lua_State *L) { | 309 | static int luaB_load (lua_State *L) { |
308 | int status; | 310 | int status; |
309 | size_t l; | 311 | size_t l; |
310 | int top = lua_gettop(L); | ||
311 | const char *s = lua_tolstring(L, 1, &l); | 312 | const char *s = lua_tolstring(L, 1, &l); |
312 | const char *mode = luaL_optstring(L, 3, "bt"); | 313 | const char *mode = luaL_optstring(L, 3, "bt"); |
314 | int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */ | ||
313 | if (s != NULL) { /* loading a string? */ | 315 | if (s != NULL) { /* loading a string? */ |
314 | const char *chunkname = luaL_optstring(L, 2, s); | 316 | const char *chunkname = luaL_optstring(L, 2, s); |
315 | status = luaL_loadbufferx(L, s, l, chunkname, mode); | 317 | status = luaL_loadbufferx(L, s, l, chunkname, mode); |
@@ -320,11 +322,7 @@ static int luaB_load (lua_State *L) { | |||
320 | lua_settop(L, RESERVEDSLOT); /* create reserved slot */ | 322 | lua_settop(L, RESERVEDSLOT); /* create reserved slot */ |
321 | status = lua_load(L, generic_reader, NULL, chunkname, mode); | 323 | status = lua_load(L, generic_reader, NULL, chunkname, mode); |
322 | } | 324 | } |
323 | if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */ | 325 | return load_aux(L, status, env); |
324 | lua_pushvalue(L, 4); /* environment for loaded function */ | ||
325 | lua_setupvalue(L, -2, 1); /* set it as 1st upvalue */ | ||
326 | } | ||
327 | return load_aux(L, status); | ||
328 | } | 326 | } |
329 | 327 | ||
330 | /* }====================================================== */ | 328 | /* }====================================================== */ |