diff options
| -rw-r--r-- | lstrlib.c | 14 | ||||
| -rw-r--r-- | testes/strings.lua | 7 |
2 files changed, 5 insertions, 16 deletions
| @@ -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 | |||
| 50 | static int str_len (lua_State *L) { | 40 | static 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) == '') | |||
| 109 | assert(string.rep('tés\00tê', 2) == 'tés\0têtés\000tê') | 109 | assert(string.rep('tés\00tê', 2) == 'tés\0têtés\000tê') |
| 110 | assert(string.rep('', 10) == '') | 110 | assert(string.rep('', 10) == '') |
| 111 | 111 | ||
| 112 | if string.packsize("i") == 4 then | 112 | do |
| 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), ',') | ||
| 116 | end | 115 | end |
| 117 | 116 | ||
| 118 | -- repetitions with separator | 117 | -- repetitions with separator |
