Skip to content

Commit b73a97e

Browse files
committed
Removed limit of 1000 wildcards in a regular expression
1 parent eb4880e commit b73a97e

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

regexp.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,23 @@ pcre_extra * extra;
4242
re->m_extra = extra;
4343
re->m_iExecutionError = 0; // no error now
4444

45+
// inspired by a suggestion by Twisol (to remove a hard-coded limit on the number of wildcards)
46+
int capturecount = 0;
47+
// how many captures did we get?
48+
pcre_fullinfo(program, NULL, PCRE_INFO_CAPTURECOUNT, &capturecount);
49+
// allocate memory for them
50+
re->m_vOffsets.resize ((capturecount + 1) * 3); // add 1 for the whole expression
51+
4552
return re;
4653
}
4754

48-
#define MAX_PCRE_WILDCARDS 1000
49-
5055
int regexec(register t_regexp *prog,
5156
register const char *string,
5257
const int start_offset)
5358
{
5459
int options = App.m_bRegexpMatchEmpty ? 0 : PCRE_NOTEMPTY; // don't match on an empty string
5560
int count;
56-
static int offsets [MAX_PCRE_WILDCARDS * 3]; // hopefully we won't recurse and crash ;)
61+
5762

5863
LARGE_INTEGER start,
5964
finish;
@@ -67,7 +72,7 @@ LARGE_INTEGER start,
6772

6873
pcre_callout = NULL;
6974
count = pcre_exec(prog->m_program, prog->m_extra, string, strlen (string),
70-
start_offset, options, offsets, NUMITEMS (offsets));
75+
start_offset, options, &prog->m_vOffsets [0], prog->m_vOffsets.size ());
7176

7277
if (App.m_iCounterFrequency)
7378
{
@@ -83,6 +88,8 @@ LARGE_INTEGER start,
8388
{
8489
pcre_free (prog->m_program);
8590
prog->m_program = NULL;
91+
pcre_free (prog->m_extra);
92+
prog->m_extra = NULL;
8693
prog->m_iExecutionError = count; // remember reason
8794
}
8895

@@ -99,9 +106,6 @@ LARGE_INTEGER start,
99106

100107
prog->m_sTarget = string; // for extracting wildcards
101108
prog->m_iCount = count; // ditto
102-
prog->m_vOffsets.clear ();
103-
// only need first 2/3 of offsets
104-
copy (offsets, &offsets [count * 2], back_inserter (prog->m_vOffsets));
105109

106110
return true; // match
107111
}

scripting/lua_scripting.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,10 @@ bool CScriptEngine::ExecuteLua (DISPID & dispid, // dispatch ID, will be set to
468468
paramCount++; // we have one more parameter to the call
469469
pcre_fullinfo(regexp->m_program, regexp->m_extra, PCRE_INFO_CAPTURECOUNT, &ncapt);
470470

471-
int iTot = regexp->m_vOffsets.size (); // how many did we actually get?
472-
473471
for (i = 0; i <= ncapt; i++)
474472
{
475473
int j = i * 2;
476-
if (j < iTot) // save if available, otherwise push a false value
477474
lua_pushlstring(L, regexp->m_sTarget.c_str () + regexp->m_vOffsets[j], regexp->m_vOffsets[j + 1] - regexp->m_vOffsets[j]);
478-
else
479-
lua_pushboolean (L, 0);
480475
lua_rawseti (L, -2, i);
481476
}
482477
// now add item 0 - the whole matching line
@@ -508,7 +503,7 @@ bool CScriptEngine::ExecuteLua (DISPID & dispid, // dispatch ID, will be set to
508503
if (found_strings.find (sName) != found_strings.end ())
509504
{
510505
// do not replace if this one is out of range
511-
if (n < 0 || n > ncapt || j >= iTot)
506+
if (n < 0 || n > ncapt)
512507
continue;
513508
} // end of duplicate
514509
else
@@ -517,12 +512,7 @@ bool CScriptEngine::ExecuteLua (DISPID & dispid, // dispatch ID, will be set to
517512

518513
lua_pushstring (L, (LPCTSTR) name);
519514
if (n >= 0 && n <= ncapt)
520-
{
521-
if (j < iTot) // save if available, otherwise push a false value
522-
lua_pushlstring(L, regexp->m_sTarget.c_str () + regexp->m_vOffsets[j], regexp->m_vOffsets[j + 1] - regexp->m_vOffsets[j]);
523-
else
524-
lua_pushboolean(L, 0);
525-
}
515+
lua_pushlstring(L, regexp->m_sTarget.c_str () + regexp->m_vOffsets[j], regexp->m_vOffsets[j + 1] - regexp->m_vOffsets[j]);
526516
else
527517
lua_pushnil (L); /* n out of range */
528518
lua_settable (L, -3);

0 commit comments

Comments
 (0)