aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-03-12 15:09:06 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-03-12 15:09:06 -0300
commitad40bb1181d08821af6789147a28aa8370533d11 (patch)
treedfea4f987f79b98dda92e0ce6a97dc738fdd2f3c /lstrlib.c
parent80fe8504f5d7be0a3ddf939981bf4b18266861b6 (diff)
downloadlua-ad40bb1181d08821af6789147a28aa8370533d11.tar.gz
lua-ad40bb1181d08821af6789147a28aa8370533d11.tar.bz2
lua-ad40bb1181d08821af6789147a28aa8370533d11.zip
detail in string.rep: allow large repetitions of the empty string (no
possibility of overflows)
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 9bafa5e4..46b06568 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.185 2014/01/09 16:21:28 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.186 2014/02/25 14:30:21 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*/
@@ -116,7 +116,7 @@ static int str_rep (lua_State *L) {
116 lua_Integer n = luaL_checkinteger(L, 2); 116 lua_Integer n = luaL_checkinteger(L, 2);
117 const char *sep = luaL_optlstring(L, 3, "", &lsep); 117 const char *sep = luaL_optlstring(L, 3, "", &lsep);
118 if (n <= 0) lua_pushliteral(L, ""); 118 if (n <= 0) lua_pushliteral(L, "");
119 else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */ 119 else if (l + lsep < l || l + lsep > MAXSIZE / n) /* may overflow? */
120 return luaL_error(L, "resulting string too large"); 120 return luaL_error(L, "resulting string too large");
121 else { 121 else {
122 size_t totallen = n * l + (n - 1) * lsep; 122 size_t totallen = n * l + (n - 1) * lsep;
@@ -124,8 +124,9 @@ static int str_rep (lua_State *L) {
124 char *p = luaL_buffinitsize(L, &b, totallen); 124 char *p = luaL_buffinitsize(L, &b, totallen);
125 while (n-- > 1) { /* first n-1 copies (followed by separator) */ 125 while (n-- > 1) { /* first n-1 copies (followed by separator) */
126 memcpy(p, s, l * sizeof(char)); p += l; 126 memcpy(p, s, l * sizeof(char)); p += l;
127 if (lsep > 0) { /* avoid empty 'memcpy' (may be expensive) */ 127 if (lsep > 0) { /* empty 'memcpy' is not that cheap */
128 memcpy(p, sep, lsep * sizeof(char)); p += lsep; 128 memcpy(p, sep, lsep * sizeof(char));
129 p += lsep;
129 } 130 }
130 } 131 }
131 memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */ 132 memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */