** MAX_SIZE is limited both by size_t and lua_Integer. ** When x <= MAX_SIZE, x can be safely cast to size_t or lua_Integer. */
| 137 | ** When x <= MAX_SIZE, x can be safely cast to size_t or lua_Integer. |
| 138 | */ |
| 139 | static int str_rep (lua_State *L) { |
| 140 | size_t len, lsep; |
| 141 | const char *s = luaL_checklstring(L, 1, &len); |
| 142 | lua_Integer n = luaL_checkinteger(L, 2); |
| 143 | const char *sep = luaL_optlstring(L, 3, "", &lsep); |
| 144 | if (n <= 0) |
| 145 | lua_pushliteral(L, ""); |
| 146 | else if (l_unlikely(len > MAX_SIZE - lsep || |
| 147 | cast_st2S(len + lsep) > cast_st2S(MAX_SIZE) / n)) |
| 148 | return luaL_error(L, "resulting string too large"); |
| 149 | else { |
| 150 | size_t totallen = (cast_sizet(n) * (len + lsep)) - lsep; |
| 151 | luaL_Buffer b; |
| 152 | char *p = luaL_buffinitsize(L, &b, totallen); |
| 153 | while (n-- > 1) { /* first n-1 copies (followed by separator) */ |
| 154 | memcpy(p, s, len * sizeof(char)); p += len; |
| 155 | if (lsep > 0) { /* empty 'memcpy' is not that cheap */ |
| 156 | memcpy(p, sep, lsep * sizeof(char)); p += lsep; |
| 157 | } |
| 158 | } |
| 159 | memcpy(p, s, len * sizeof(char)); /* last copy without separator */ |
| 160 | luaL_pushresultsize(&b, totallen); |
| 161 | } |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | static int str_byte (lua_State *L) { |
nothing calls this directly
no test coverage detected