diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-07-18 14:44:40 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-07-18 14:44:40 -0300 |
commit | a546138d158d79d44b2c5b42630be00d306f4e7c (patch) | |
tree | 8d2410921dc1af4dc8e7d19b21d3f313bcc812b4 | |
parent | cd4de92762434e6ed0e6c207d56d365300396dd8 (diff) | |
download | lua-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.c | 9 | ||||
-rw-r--r-- | lcode.c | 2 | ||||
-rw-r--r-- | manual/manual.of | 10 |
3 files changed, 17 insertions, 4 deletions
@@ -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 | ||
1031 | LUA_API void lua_callk (lua_State *L, int nargs, int nresults, | 1036 | LUA_API void lua_callk (lua_State *L, int nargs, int nresults, |
@@ -724,6 +724,8 @@ static void const2exp (TValue *v, expdesc *e) { | |||
724 | */ | 724 | */ |
725 | void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { | 725 | void 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, | |||
3028 | all arguments and the function value are popped | 3028 | all arguments and the function value are popped |
3029 | and the call results are pushed onto the stack. | 3029 | and the call results are pushed onto the stack. |
3030 | The number of results is adjusted to @id{nresults}, | 3030 | The number of results is adjusted to @id{nresults}, |
3031 | unless @id{nresults} is @defid{LUA_MULTRET}. | 3031 | unless @id{nresults} is @defid{LUA_MULTRET}, |
3032 | In this case, all results from the function are pushed; | 3032 | which makes all results from the function to be pushed. |
3033 | In the first case, an explicit number of results, | ||
3034 | the caller must ensure that the stack has space for the | ||
3035 | returned values. | ||
3036 | In the second case, all results, | ||
3033 | Lua takes care that the returned values fit into the stack space, | 3037 | Lua takes care that the returned values fit into the stack space, |
3034 | but it does not ensure any extra space in the stack. | 3038 | but it does not ensure any extra space in the stack. |
3035 | The function results are pushed onto the stack in direct order | 3039 | The function results are pushed onto the stack in direct order |
3036 | (the first result is pushed first), | 3040 | (the first result is pushed first), |
3037 | so that after the call the last result is on the top of the stack. | 3041 | so that after the call the last result is on the top of the stack. |
3038 | 3042 | ||
3043 | The maximum value for @id{nresults} is 250. | ||
3044 | |||
3039 | Any error while calling and running the function is propagated upwards | 3045 | Any error while calling and running the function is propagated upwards |
3040 | (with a @id{longjmp}). | 3046 | (with a @id{longjmp}). |
3041 | 3047 | ||