Add check for too complex patterns.

```
-- bug since 2.5 (C-stack overflow)
do
  local function f (size)
    local s = string.rep("a", size)
    local p = string.rep(".?", size)
    return pcall(string.match, s, p)
  end
  local r, m = f(80)
  assert(r and #m == 80)
  r, m = f(200000)
  assert(not r and string.find(m, "too complex"), tostring(r)..", "..tostring(m))
end
```
This commit is contained in:
Enyby
2019-11-09 23:25:07 +02:00
parent 5813d56f89
commit 0f0ec4bf7b

View File

@@ -803,6 +803,8 @@ public class StringLib extends TwoArgFunction {
private static final LuaString SPECIALS = valueOf("^$*+?.([%-"); private static final LuaString SPECIALS = valueOf("^$*+?.([%-");
private static final int MAX_CAPTURES = 32; private static final int MAX_CAPTURES = 32;
private static final int MAXCCALLS = 200;
private static final int CAP_UNFINISHED = -1; private static final int CAP_UNFINISHED = -1;
private static final int CAP_POSITION = -2; private static final int CAP_POSITION = -2;
@@ -846,6 +848,7 @@ public class StringLib extends TwoArgFunction {
}; };
static class MatchState { static class MatchState {
int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
final LuaString s; final LuaString s;
final LuaString p; final LuaString p;
final Varargs args; final Varargs args;
@@ -860,10 +863,12 @@ public class StringLib extends TwoArgFunction {
this.level = 0; this.level = 0;
this.cinit = new int[ MAX_CAPTURES ]; this.cinit = new int[ MAX_CAPTURES ];
this.clen = new int[ MAX_CAPTURES ]; this.clen = new int[ MAX_CAPTURES ];
this.matchdepth = MAXCCALLS;
} }
void reset() { void reset() {
level = 0; level = 0;
this.matchdepth = MAXCCALLS;
} }
private void add_s( Buffer lbuf, LuaString news, int soff, int e ) { private void add_s( Buffer lbuf, LuaString news, int soff, int e ) {
@@ -1052,6 +1057,8 @@ public class StringLib extends TwoArgFunction {
* where match ends, otherwise returns -1. * where match ends, otherwise returns -1.
*/ */
int match( int soffset, int poffset ) { int match( int soffset, int poffset ) {
if (matchdepth-- == 0) error("pattern too complex");
try {
while ( true ) { while ( true ) {
// Check if we are at the end of the pattern - // Check if we are at the end of the pattern -
// equivalent to the '\0' case in the C version, but our pattern // equivalent to the '\0' case in the C version, but our pattern
@@ -1128,6 +1135,9 @@ public class StringLib extends TwoArgFunction {
continue; continue;
} }
} }
} finally {
matchdepth++;
}
} }
int max_expand( int soff, int poff, int ep ) { int max_expand( int soff, int poff, int ep ) {