** Try to grow the stack by at least 'n' elements. When 'raiseerror' ** is true, raises any error; otherwise, return 0 in case of errors. */
| 240 | ** is true, raises any error; otherwise, return 0 in case of errors. |
| 241 | */ |
| 242 | int luaD_growstack (lua_State *L, int n, int raiseerror) { |
| 243 | int size = stacksize(L); |
| 244 | if (l_unlikely(size > LUAI_MAXSTACK)) { |
| 245 | /* if stack is larger than maximum, thread is already using the |
| 246 | extra space reserved for errors, that is, thread is handling |
| 247 | a stack error; cannot grow further than that. */ |
| 248 | lua_assert(stacksize(L) == ERRORSTACKSIZE); |
| 249 | if (raiseerror) |
| 250 | luaD_throw(L, LUA_ERRERR); /* error inside message handler */ |
| 251 | return 0; /* if not 'raiseerror', just signal it */ |
| 252 | } |
| 253 | else if (n < LUAI_MAXSTACK) { /* avoids arithmetic overflows */ |
| 254 | int newsize = 2 * size; /* tentative new size */ |
| 255 | int needed = cast_int(L->top.p - L->stack.p) + n; |
| 256 | if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ |
| 257 | newsize = LUAI_MAXSTACK; |
| 258 | if (newsize < needed) /* but must respect what was asked for */ |
| 259 | newsize = needed; |
| 260 | if (l_likely(newsize <= LUAI_MAXSTACK)) |
| 261 | return luaD_reallocstack(L, newsize, raiseerror); |
| 262 | } |
| 263 | /* else stack overflow */ |
| 264 | /* add extra size to be able to handle the error message */ |
| 265 | luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); |
| 266 | if (raiseerror) |
| 267 | luaG_runerror(L, "stack overflow"); |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | /* |
no test coverage detected