aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-06-21 16:26:49 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-06-21 16:26:49 -0300
commitef28e5f789f7e7be1a3961d13cb35bbfd2542997 (patch)
tree4f254fc849428d984de947dae1bf344be674347c
parentec65ab878e04822f1cbcc3198f19076d57900e9f (diff)
downloadlua-ef28e5f789f7e7be1a3961d13cb35bbfd2542997.tar.gz
lua-ef28e5f789f7e7be1a3961d13cb35bbfd2542997.tar.bz2
lua-ef28e5f789f7e7be1a3961d13cb35bbfd2542997.zip
Removed 'int' size limit for string.rep
-rw-r--r--lstrlib.c14
-rw-r--r--testes/strings.lua7
2 files changed, 5 insertions, 16 deletions
diff --git a/lstrlib.c b/lstrlib.c
index eb38b67d..8d6573a6 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -37,16 +37,6 @@
37#endif 37#endif
38 38
39 39
40/*
41** Some sizes are better limited to fit in 'int', but must also fit in
42** 'size_t'. (We assume that 'lua_Integer' cannot be smaller than 'int'.)
43*/
44#define MAXSIZE \
45 (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX))
46
47
48
49
50static int str_len (lua_State *L) { 40static int str_len (lua_State *L) {
51 size_t l; 41 size_t l;
52 luaL_checklstring(L, 1, &l); 42 luaL_checklstring(L, 1, &l);
@@ -149,10 +139,10 @@ static int str_rep (lua_State *L) {
149 const char *sep = luaL_optlstring(L, 3, "", &lsep); 139 const char *sep = luaL_optlstring(L, 3, "", &lsep);
150 if (n <= 0) 140 if (n <= 0)
151 lua_pushliteral(L, ""); 141 lua_pushliteral(L, "");
152 else if (l_unlikely(l + lsep < l || l + lsep > MAXSIZE / n)) 142 else if (l_unlikely(l + lsep < l || l + lsep > MAX_SIZE / n))
153 return luaL_error(L, "resulting string too large"); 143 return luaL_error(L, "resulting string too large");
154 else { 144 else {
155 size_t totallen = (size_t)n * l + (size_t)(n - 1) * lsep; 145 size_t totallen = ((size_t)n * (l + lsep)) - lsep;
156 luaL_Buffer b; 146 luaL_Buffer b;
157 char *p = luaL_buffinitsize(L, &b, totallen); 147 char *p = luaL_buffinitsize(L, &b, totallen);
158 while (n-- > 1) { /* first n-1 copies (followed by separator) */ 148 while (n-- > 1) { /* first n-1 copies (followed by separator) */
diff --git a/testes/strings.lua b/testes/strings.lua
index c124b369..a0204309 100644
--- a/testes/strings.lua
+++ b/testes/strings.lua
@@ -109,10 +109,9 @@ assert(string.rep('teste', 0) == '')
109assert(string.rep('tés\00tê', 2) == 'tés\0têtés\000tê') 109assert(string.rep('tés\00tê', 2) == 'tés\0têtés\000tê')
110assert(string.rep('', 10) == '') 110assert(string.rep('', 10) == '')
111 111
112if string.packsize("i") == 4 then 112do
113 -- result length would be 2^31 (int overflow) 113 checkerror("too large", string.rep, 'aa', math.maxinteger);
114 checkerror("too large", string.rep, 'aa', (1 << 30)) 114 checkerror("too large", string.rep, 'a', math.maxinteger/2, ',')
115 checkerror("too large", string.rep, 'a', (1 << 30), ',')
116end 115end
117 116
118-- repetitions with separator 117-- repetitions with separator