diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-03-12 15:09:06 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-03-12 15:09:06 -0300 |
commit | ad40bb1181d08821af6789147a28aa8370533d11 (patch) | |
tree | dfea4f987f79b98dda92e0ce6a97dc738fdd2f3c /lstrlib.c | |
parent | 80fe8504f5d7be0a3ddf939981bf4b18266861b6 (diff) | |
download | lua-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.c | 9 |
1 files changed, 5 insertions, 4 deletions
@@ -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) */ |