aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-07-18 14:44:40 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-07-18 14:44:40 -0300
commita546138d158d79d44b2c5b42630be00d306f4e7c (patch)
tree8d2410921dc1af4dc8e7d19b21d3f313bcc812b4
parentcd4de92762434e6ed0e6c207d56d365300396dd8 (diff)
downloadlua-a546138d158d79d44b2c5b42630be00d306f4e7c.tar.gz
lua-a546138d158d79d44b2c5b42630be00d306f4e7c.tar.bz2
lua-a546138d158d79d44b2c5b42630be00d306f4e7c.zip
Explicit limit for number of results in a call
The parameter 'nresults' in 'lua_call' and similar functions has a limit of 250. It already had an undocumented (and unchecked) limit of SHRT_MAX, but it is seldom larger than 2.
-rw-r--r--lapi.c9
-rw-r--r--lcode.c2
-rw-r--r--manual/manual.of10
3 files changed, 17 insertions, 4 deletions
diff --git a/lapi.c b/lapi.c
index f00bd53f..dbd291d7 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1022,10 +1022,15 @@ LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
1022*/ 1022*/
1023 1023
1024 1024
1025#define MAXRESULTS 250
1026
1027
1025#define checkresults(L,na,nr) \ 1028#define checkresults(L,na,nr) \
1026 api_check(L, (nr) == LUA_MULTRET \ 1029 (api_check(L, (nr) == LUA_MULTRET \
1027 || (L->ci->top.p - L->top.p >= (nr) - (na)), \ 1030 || (L->ci->top.p - L->top.p >= (nr) - (na)), \
1028 "results from function overflow current stack size") 1031 "results from function overflow current stack size"), \
1032 api_check(L, LUA_MULTRET <= (nr) && (nr) <= MAXRESULTS, \
1033 "invalid number of results"))
1029 1034
1030 1035
1031LUA_API void lua_callk (lua_State *L, int nargs, int nresults, 1036LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
diff --git a/lcode.c b/lcode.c
index c1fce37f..0799306e 100644
--- a/lcode.c
+++ b/lcode.c
@@ -724,6 +724,8 @@ static void const2exp (TValue *v, expdesc *e) {
724*/ 724*/
725void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { 725void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {
726 Instruction *pc = &getinstruction(fs, e); 726 Instruction *pc = &getinstruction(fs, e);
727 if (nresults + 1 > MAXARG_C)
728 luaX_syntaxerror(fs->ls, "too many multiple results");
727 if (e->k == VCALL) /* expression is an open function call? */ 729 if (e->k == VCALL) /* expression is an open function call? */
728 SETARG_C(*pc, nresults + 1); 730 SETARG_C(*pc, nresults + 1);
729 else { 731 else {
diff --git a/manual/manual.of b/manual/manual.of
index 1069f644..c7f6904a 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -3028,14 +3028,20 @@ When the function returns,
3028all arguments and the function value are popped 3028all arguments and the function value are popped
3029and the call results are pushed onto the stack. 3029and the call results are pushed onto the stack.
3030The number of results is adjusted to @id{nresults}, 3030The number of results is adjusted to @id{nresults},
3031unless @id{nresults} is @defid{LUA_MULTRET}. 3031unless @id{nresults} is @defid{LUA_MULTRET},
3032In this case, all results from the function are pushed; 3032which makes all results from the function to be pushed.
3033In the first case, an explicit number of results,
3034the caller must ensure that the stack has space for the
3035returned values.
3036In the second case, all results,
3033Lua takes care that the returned values fit into the stack space, 3037Lua takes care that the returned values fit into the stack space,
3034but it does not ensure any extra space in the stack. 3038but it does not ensure any extra space in the stack.
3035The function results are pushed onto the stack in direct order 3039The function results are pushed onto the stack in direct order
3036(the first result is pushed first), 3040(the first result is pushed first),
3037so that after the call the last result is on the top of the stack. 3041so that after the call the last result is on the top of the stack.
3038 3042
3043The maximum value for @id{nresults} is 250.
3044
3039Any error while calling and running the function is propagated upwards 3045Any error while calling and running the function is propagated upwards
3040(with a @id{longjmp}). 3046(with a @id{longjmp}).
3041 3047