diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-02-06 16:29:03 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-02-06 16:29:03 -0200 |
commit | f4e762f688935228dd9aa6b2f9e28a5a34f385b1 (patch) | |
tree | c7105513446854c363cf11d276b6e6ac4fe818ba | |
parent | 1ce57628b21433c0cf7e418b0359766e241106ce (diff) | |
download | lua-f4e762f688935228dd9aa6b2f9e28a5a34f385b1.tar.gz lua-f4e762f688935228dd9aa6b2f9e28a5a34f385b1.tar.bz2 lua-f4e762f688935228dd9aa6b2f9e28a5a34f385b1.zip |
better error checking for 'table.insert' and 'table.remove'
-rw-r--r-- | ltablib.c | 22 |
1 files changed, 12 insertions, 10 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltablib.c,v 1.62 2011/09/30 12:45:45 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.63 2011/11/28 17:26:30 roberto Exp roberto $ |
3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -16,8 +16,8 @@ | |||
16 | #include "lualib.h" | 16 | #include "lualib.h" |
17 | 17 | ||
18 | 18 | ||
19 | #define aux_getn(L,n) \ | 19 | #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) |
20 | (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) | 20 | |
21 | 21 | ||
22 | 22 | ||
23 | #if defined(LUA_COMPAT_MAXN) | 23 | #if defined(LUA_COMPAT_MAXN) |
@@ -49,7 +49,7 @@ static int tinsert (lua_State *L) { | |||
49 | case 3: { | 49 | case 3: { |
50 | int i; | 50 | int i; |
51 | pos = luaL_checkint(L, 2); /* 2nd argument is the position */ | 51 | pos = luaL_checkint(L, 2); /* 2nd argument is the position */ |
52 | if (pos > e) e = pos; /* `grow' array if necessary */ | 52 | luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); |
53 | for (i = e; i > pos; i--) { /* move up elements */ | 53 | for (i = e; i > pos; i--) { /* move up elements */ |
54 | lua_rawgeti(L, 1, i-1); | 54 | lua_rawgeti(L, 1, i-1); |
55 | lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ | 55 | lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ |
@@ -66,17 +66,19 @@ static int tinsert (lua_State *L) { | |||
66 | 66 | ||
67 | 67 | ||
68 | static int tremove (lua_State *L) { | 68 | static int tremove (lua_State *L) { |
69 | int e = aux_getn(L, 1); | 69 | int size = aux_getn(L, 1); |
70 | int pos = luaL_optint(L, 2, e); | 70 | int pos = luaL_optint(L, 2, size); |
71 | if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ | 71 | if (pos != size) /* validate 'pos' if given */ |
72 | return 0; /* nothing to remove */ | 72 | luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); |
73 | else if (size == 0) /* empty table? */ | ||
74 | return 0; /* return nothing (nil) */ | ||
73 | lua_rawgeti(L, 1, pos); /* result = t[pos] */ | 75 | lua_rawgeti(L, 1, pos); /* result = t[pos] */ |
74 | for ( ;pos<e; pos++) { | 76 | for ( ; pos < size; pos++) { |
75 | lua_rawgeti(L, 1, pos+1); | 77 | lua_rawgeti(L, 1, pos+1); |
76 | lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ | 78 | lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ |
77 | } | 79 | } |
78 | lua_pushnil(L); | 80 | lua_pushnil(L); |
79 | lua_rawseti(L, 1, e); /* t[e] = nil */ | 81 | lua_rawseti(L, 1, pos); /* t[pos] = nil */ |
80 | return 1; | 82 | return 1; |
81 | } | 83 | } |
82 | 84 | ||