diff options
-rw-r--r-- | bugs | 25 | ||||
-rw-r--r-- | ltablib.c | 5 |
2 files changed, 28 insertions, 2 deletions
@@ -1570,6 +1570,31 @@ lstrlib.c: | |||
1570 | } | 1570 | } |
1571 | 1571 | ||
1572 | Bug{ | 1572 | Bug{ |
1573 | what = [[table.remove removes last element of a table when given | ||
1574 | an out-of-bound index]], | ||
1575 | report = [[Patrick Donnelly, on 11/2007]], | ||
1576 | since = [[at least 5.0]], | ||
1577 | example = [[ | ||
1578 | a = {1,2,3} | ||
1579 | table.remove(a, 4) | ||
1580 | print(a[3]) --> nil (should be 3) | ||
1581 | ]], | ||
1582 | patch = [[ | ||
1583 | ltablib.c: | ||
1584 | @@ -118,7 +118,8 @@ | ||
1585 | static int tremove (lua_State *L) { | ||
1586 | int e = aux_getn(L, 1); | ||
1587 | int pos = luaL_optint(L, 2, e); | ||
1588 | - if (e == 0) return 0; /* table is `empty' */ | ||
1589 | + if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ | ||
1590 | + return 0; /* nothing to remove */ | ||
1591 | luaL_setn(L, 1, e - 1); /* t.n = n-1 */ | ||
1592 | lua_rawgeti(L, 1, pos); /* result = t[pos] */ | ||
1593 | for ( ;pos<e; pos++) { | ||
1594 | ]], | ||
1595 | } | ||
1596 | |||
1597 | Bug{ | ||
1573 | what = [[ ]], | 1598 | what = [[ ]], |
1574 | report = [[ , on ]], | 1599 | report = [[ , on ]], |
1575 | since = [[i ]], | 1600 | since = [[i ]], |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltablib.c,v 1.40 2007/06/21 13:50:53 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.41 2007/09/12 20:53:24 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 | */ |
@@ -110,7 +110,8 @@ static int tinsert (lua_State *L) { | |||
110 | static int tremove (lua_State *L) { | 110 | static int tremove (lua_State *L) { |
111 | int e = aux_getn(L, 1); | 111 | int e = aux_getn(L, 1); |
112 | int pos = luaL_optint(L, 2, e); | 112 | int pos = luaL_optint(L, 2, e); |
113 | if (e == 0) return 0; /* table is `empty' */ | 113 | if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ |
114 | return 0; /* nothing to remove */ | ||
114 | lua_rawgeti(L, 1, pos); /* result = t[pos] */ | 115 | lua_rawgeti(L, 1, pos); /* result = t[pos] */ |
115 | for ( ;pos<e; pos++) { | 116 | for ( ;pos<e; pos++) { |
116 | lua_rawgeti(L, 1, pos+1); | 117 | lua_rawgeti(L, 1, pos+1); |