diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-06-19 11:29:01 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-06-19 11:29:01 -0300 |
commit | 55f566bd2270503d098404874d701f2f30abd0d3 (patch) | |
tree | 054bb60985457352d41d677f4fe7fcc6f24d6731 | |
parent | 130c0e40e01e24a79f7e416d1cc4187cd4a2a5fc (diff) | |
download | lua-55f566bd2270503d098404874d701f2f30abd0d3.tar.gz lua-55f566bd2270503d098404874d701f2f30abd0d3.tar.bz2 lua-55f566bd2270503d098404874d701f2f30abd0d3.zip |
use lua_Integer for integer parameters to avoid truncation
-rw-r--r-- | lstrlib.c | 30 |
1 files changed, 15 insertions, 15 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.179 2013/04/25 13:52:13 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.180 2013/06/07 14:51:10 roberto Exp roberto $ |
3 | ** Standard library for string operations and pattern-matching | 3 | ** Standard library for string operations and pattern-matching |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -43,20 +43,20 @@ static int str_len (lua_State *L) { | |||
43 | 43 | ||
44 | 44 | ||
45 | /* translate a relative string position: negative means back from end */ | 45 | /* translate a relative string position: negative means back from end */ |
46 | static size_t posrelat (ptrdiff_t pos, size_t len) { | 46 | static lua_Integer posrelat (lua_Integer pos, size_t len) { |
47 | if (pos >= 0) return (size_t)pos; | 47 | if (pos >= 0) return pos; |
48 | else if (0u - (size_t)pos > len) return 0; | 48 | else if (0u - (size_t)pos > len) return 0; |
49 | else return len - ((size_t)-pos) + 1; | 49 | else return (lua_Integer)len + pos + 1; |
50 | } | 50 | } |
51 | 51 | ||
52 | 52 | ||
53 | static int str_sub (lua_State *L) { | 53 | static int str_sub (lua_State *L) { |
54 | size_t l; | 54 | size_t l; |
55 | const char *s = luaL_checklstring(L, 1, &l); | 55 | const char *s = luaL_checklstring(L, 1, &l); |
56 | size_t start = posrelat(luaL_checkinteger(L, 2), l); | 56 | lua_Integer start = posrelat(luaL_checkinteger(L, 2), l); |
57 | size_t end = posrelat(luaL_optinteger(L, 3, -1), l); | 57 | lua_Integer end = posrelat(luaL_optinteger(L, 3, -1), l); |
58 | if (start < 1) start = 1; | 58 | if (start < 1) start = 1; |
59 | if (end > l) end = l; | 59 | if (end > (lua_Integer)l) end = l; |
60 | if (start <= end) | 60 | if (start <= end) |
61 | lua_pushlstring(L, s + start - 1, end - start + 1); | 61 | lua_pushlstring(L, s + start - 1, end - start + 1); |
62 | else lua_pushliteral(L, ""); | 62 | else lua_pushliteral(L, ""); |
@@ -108,7 +108,7 @@ static int str_upper (lua_State *L) { | |||
108 | static int str_rep (lua_State *L) { | 108 | static int str_rep (lua_State *L) { |
109 | size_t l, lsep; | 109 | size_t l, lsep; |
110 | const char *s = luaL_checklstring(L, 1, &l); | 110 | const char *s = luaL_checklstring(L, 1, &l); |
111 | int n = luaL_checkint(L, 2); | 111 | lua_Integer n = luaL_checkinteger(L, 2); |
112 | const char *sep = luaL_optlstring(L, 3, "", &lsep); | 112 | const char *sep = luaL_optlstring(L, 3, "", &lsep); |
113 | if (n <= 0) lua_pushliteral(L, ""); | 113 | if (n <= 0) lua_pushliteral(L, ""); |
114 | else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */ | 114 | else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */ |
@@ -133,14 +133,14 @@ static int str_rep (lua_State *L) { | |||
133 | static int str_byte (lua_State *L) { | 133 | static int str_byte (lua_State *L) { |
134 | size_t l; | 134 | size_t l; |
135 | const char *s = luaL_checklstring(L, 1, &l); | 135 | const char *s = luaL_checklstring(L, 1, &l); |
136 | size_t posi = posrelat(luaL_optinteger(L, 2, 1), l); | 136 | lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), l); |
137 | size_t pose = posrelat(luaL_optinteger(L, 3, posi), l); | 137 | lua_Integer pose = posrelat(luaL_optinteger(L, 3, posi), l); |
138 | int n, i; | 138 | int n, i; |
139 | if (posi < 1) posi = 1; | 139 | if (posi < 1) posi = 1; |
140 | if (pose > l) pose = l; | 140 | if (pose > (lua_Integer)l) pose = l; |
141 | if (posi > pose) return 0; /* empty interval; return no values */ | 141 | if (posi > pose) return 0; /* empty interval; return no values */ |
142 | n = (int)(pose - posi + 1); | 142 | n = (int)(pose - posi + 1); |
143 | if (posi + n <= pose) /* (size_t -> int) overflow? */ | 143 | if (posi + n <= pose) /* arithmetic overflow? */ |
144 | return luaL_error(L, "string slice too long"); | 144 | return luaL_error(L, "string slice too long"); |
145 | luaL_checkstack(L, n, "string slice too long"); | 145 | luaL_checkstack(L, n, "string slice too long"); |
146 | for (i=0; i<n; i++) | 146 | for (i=0; i<n; i++) |
@@ -155,7 +155,7 @@ static int str_char (lua_State *L) { | |||
155 | luaL_Buffer b; | 155 | luaL_Buffer b; |
156 | char *p = luaL_buffinitsize(L, &b, n); | 156 | char *p = luaL_buffinitsize(L, &b, n); |
157 | for (i=1; i<=n; i++) { | 157 | for (i=1; i<=n; i++) { |
158 | int c = luaL_checkint(L, i); | 158 | lua_Integer c = luaL_checkinteger(L, i); |
159 | luaL_argcheck(L, uchar(c) == c, i, "value out of range"); | 159 | luaL_argcheck(L, uchar(c) == c, i, "value out of range"); |
160 | p[i - 1] = uchar(c); | 160 | p[i - 1] = uchar(c); |
161 | } | 161 | } |
@@ -578,9 +578,9 @@ static int str_find_aux (lua_State *L, int find) { | |||
578 | size_t ls, lp; | 578 | size_t ls, lp; |
579 | const char *s = luaL_checklstring(L, 1, &ls); | 579 | const char *s = luaL_checklstring(L, 1, &ls); |
580 | const char *p = luaL_checklstring(L, 2, &lp); | 580 | const char *p = luaL_checklstring(L, 2, &lp); |
581 | size_t init = posrelat(luaL_optinteger(L, 3, 1), ls); | 581 | lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls); |
582 | if (init < 1) init = 1; | 582 | if (init < 1) init = 1; |
583 | else if (init > ls + 1) { /* start after string's end? */ | 583 | else if (init > (lua_Integer)ls + 1) { /* start after string's end? */ |
584 | lua_pushnil(L); /* cannot find anything */ | 584 | lua_pushnil(L); /* cannot find anything */ |
585 | return 1; | 585 | return 1; |
586 | } | 586 | } |