@@ -392,6 +392,7 @@ void lua_State::hookCall(CallInfo *ci_arg) {
392392// Convert to private lua_State method
393393void lua_State::retHook (CallInfo *ci_arg, int nres) {
394394 if (getHookMask () & LUA_MASKRET) { /* is return hook on? */
395+ lua_assert (getTop ().p >= getStack ().p + nres); /* ensure nres is in bounds */
395396 StkId firstres = getTop ().p - nres; /* index of first result */
396397 int delta = 0 ; /* correction for vararg functions */
397398 int ftransfer;
@@ -426,6 +427,7 @@ unsigned lua_State::tryFuncTM(StkId func, unsigned status_val) {
426427 tm = luaT_gettmbyobj (this , s2v (func), TMS::TM_CALL);
427428 if (l_unlikely (ttisnil (tm))) /* no metamethod? */
428429 luaG_callerror (this , s2v (func));
430+ lua_assert (func >= getStack ().p && getTop ().p > func); /* ensure valid bounds */
429431 for (p = getTop ().p ; p > func; p--) /* open space for metamethod */
430432 *s2v (p) = *s2v (p-1 ); /* shift stack - use operator= */
431433 getStackSubsystem ().push (); /* stack space pre-allocated by the caller */
@@ -440,10 +442,12 @@ unsigned lua_State::tryFuncTM(StkId func, unsigned status_val) {
440442// Convert to private lua_State method
441443void lua_State::genMoveResults (StkId res, int nres,
442444 int wanted) {
445+ lua_assert (nres >= 0 && getTop ().p >= getStack ().p + nres); /* ensure nres valid */
443446 StkId firstresult = getTop ().p - nres; /* index of first result */
444447 int i;
445448 if (nres > wanted) /* extra results? */
446449 nres = wanted; /* don't need them */
450+ lua_assert (firstresult >= getStack ().p && res >= getStack ().p ); /* ensure valid pointers */
447451 for (i = 0 ; i < nres; i++) /* move all results to correct place */
448452 *s2v (res + i) = *s2v (firstresult + i); /* use operator= */
449453 for (; i < wanted; i++) /* complete wanted number of results */
0 commit comments