diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2006-03-21 16:31:09 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2006-03-21 16:31:09 -0300 |
| commit | 0238a0b01e042fad472a60138162209431b8e703 (patch) | |
| tree | 0aa20cde6a556c25c985cc7aa9ec20d56a4a76c4 | |
| parent | 1ae0b6c0bf9c389bcf9bdf266591cb1e17e80a15 (diff) | |
| download | lua-0238a0b01e042fad472a60138162209431b8e703.tar.gz lua-0238a0b01e042fad472a60138162209431b8e703.tar.bz2 lua-0238a0b01e042fad472a60138162209431b8e703.zip | |
BUG: luaL_checkudata may show wrong error message
| -rw-r--r-- | bugs | 38 | ||||
| -rw-r--r-- | lauxlib.c | 18 |
2 files changed, 50 insertions, 6 deletions
| @@ -839,3 +839,41 @@ patch = [[ | |||
| 839 | 839 | ||
| 840 | } | 840 | } |
| 841 | 841 | ||
| 842 | |||
| 843 | Bug{ | ||
| 844 | what = [[luaL_checkudata may produce wrong error message]], | ||
| 845 | |||
| 846 | report = [[Greg Falcon, 21/03/2006]], | ||
| 847 | |||
| 848 | example = [[ | ||
| 849 | getmetatable(io.stdin).__gc() | ||
| 850 | --> bad argument #1 to '__gc' (FILE* expected, got table) | ||
| 851 | ]], | ||
| 852 | |||
| 853 | patch = [[ | ||
| 854 | * lauxlib.c: | ||
| 855 | @@ -123,11 +123,17 @@ | ||
| 856 | |||
| 857 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { | ||
| 858 | void *p = lua_touserdata(L, ud); | ||
| 859 | - lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ | ||
| 860 | - if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2)) | ||
| 861 | - luaL_typerror(L, ud, tname); | ||
| 862 | - lua_pop(L, 2); /* remove both metatables */ | ||
| 863 | - return p; | ||
| 864 | + if (p != NULL) { /* value is a userdata? */ | ||
| 865 | + if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ | ||
| 866 | + lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ | ||
| 867 | + if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ | ||
| 868 | + lua_pop(L, 2); /* remove both metatables */ | ||
| 869 | + return p; | ||
| 870 | + } | ||
| 871 | + } | ||
| 872 | + } | ||
| 873 | + luaL_typerror(L, ud, tname); /* else error */ | ||
| 874 | + return NULL; /* to avoid warnings */ | ||
| 875 | } | ||
| 876 | ]] | ||
| 877 | |||
| 878 | } | ||
| 879 | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.157 2005/12/29 15:32:11 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.158 2006/01/16 12:42:21 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -123,11 +123,17 @@ LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { | |||
| 123 | 123 | ||
| 124 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { | 124 | LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { |
| 125 | void *p = lua_touserdata(L, ud); | 125 | void *p = lua_touserdata(L, ud); |
| 126 | lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ | 126 | if (p != NULL) { /* value is a userdata? */ |
| 127 | if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2)) | 127 | if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ |
| 128 | luaL_typerror(L, ud, tname); | 128 | lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ |
| 129 | lua_pop(L, 2); /* remove both metatables */ | 129 | if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ |
| 130 | return p; | 130 | lua_pop(L, 2); /* remove both metatables */ |
| 131 | return p; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | luaL_typerror(L, ud, tname); /* else error */ | ||
| 136 | return NULL; /* to avoid warnings */ | ||
| 131 | } | 137 | } |
| 132 | 138 | ||
| 133 | 139 | ||
