diff options
| -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) */ |
