diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-05-20 13:14:33 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2022-05-20 13:14:33 -0300 |
commit | 42d40581dd919fb134c07027ca1ce0844c670daf (patch) | |
tree | 9a1037510268dc09dbedebf2a91729399bcca2e0 /lvm.c | |
parent | e435aaabef8e717e0812a16a82b56acd11fb34c1 (diff) | |
download | lua-42d40581dd919fb134c07027ca1ce0844c670daf.tar.gz lua-42d40581dd919fb134c07027ca1ce0844c670daf.tar.bz2 lua-42d40581dd919fb134c07027ca1ce0844c670daf.zip |
Save stack space while handling errors
Because error handling (luaG_errormsg) uses slots from EXTRA_STACK,
and some errors can recur (e.g., string overflow while creating an
error message in 'luaG_runerror', or a C-stack overflow before calling
the message handler), the code should use stack slots with parsimony.
This commit fixes the bug "Lua-stack overflow when C stack overflows
while handling an error".
Diffstat (limited to 'lvm.c')
-rw-r--r-- | lvm.c | 6 |
1 files changed, 4 insertions, 2 deletions
@@ -656,8 +656,10 @@ void luaV_concat (lua_State *L, int total) { | |||
656 | /* collect total length and number of strings */ | 656 | /* collect total length and number of strings */ |
657 | for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { | 657 | for (n = 1; n < total && tostring(L, s2v(top - n - 1)); n++) { |
658 | size_t l = vslen(s2v(top - n - 1)); | 658 | size_t l = vslen(s2v(top - n - 1)); |
659 | if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) | 659 | if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) { |
660 | L->top = top - total; /* pop strings to avoid wasting stack */ | ||
660 | luaG_runerror(L, "string length overflow"); | 661 | luaG_runerror(L, "string length overflow"); |
662 | } | ||
661 | tl += l; | 663 | tl += l; |
662 | } | 664 | } |
663 | if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ | 665 | if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ |
@@ -672,7 +674,7 @@ void luaV_concat (lua_State *L, int total) { | |||
672 | setsvalue2s(L, top - n, ts); /* create result */ | 674 | setsvalue2s(L, top - n, ts); /* create result */ |
673 | } | 675 | } |
674 | total -= n-1; /* got 'n' strings to create 1 new */ | 676 | total -= n-1; /* got 'n' strings to create 1 new */ |
675 | L->top -= n-1; /* popped 'n' strings and pushed one */ | 677 | L->top = top - (n - 1); /* popped 'n' strings and pushed one */ |
676 | } while (total > 1); /* repeat until only 1 result left */ | 678 | } while (total > 1); /* repeat until only 1 result left */ |
677 | } | 679 | } |
678 | 680 | ||