Skip to content
Prev Previous commit
Next Next commit
6. validate in _validate_inner() / _validate_outer()
  • Loading branch information
wjssz committed Apr 3, 2022
commit db7e88b2af63d60b3bab3728e0a72a280331d02c
17 changes: 13 additions & 4 deletions Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -1473,8 +1473,8 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
self->pattern = pattern;

self->flags = flags;

self->groups = groups;
self->repeat_count = repeat_count;

if (PyDict_GET_SIZE(groupindex) > 0) {
Py_INCREF(groupindex);
Expand Down Expand Up @@ -1838,17 +1838,25 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, PatternObject *self)
case SRE_OP_REPEAT:
case SRE_OP_POSSESSIVE_REPEAT:
{
SRE_CODE op1 = op, min, max;
SRE_CODE op1 = op, min, max, repeat_index, _fields;
GET_SKIP;
GET_ARG; min = arg;
GET_ARG; max = arg;
if (min > max)
FAIL;
if (max > SRE_MAXREPEAT)
FAIL;
if (!_validate_inner(code, code+skip-3, self))
if (op1 == SRE_OP_REPEAT) {
GET_ARG; repeat_index = arg;
if (repeat_index >= (size_t)self->repeat_count)
FAIL;
_fields = 4;
Comment thread
This conversation was marked as resolved.
Outdated
} else {
_fields = 3;
}
if (!_validate_inner(code, code+skip-_fields, self))
FAIL;
code += skip-3;
code += skip-_fields;
GET_OP;
if (op1 == SRE_OP_POSSESSIVE_REPEAT) {
if (op != SRE_OP_SUCCESS)
Expand Down Expand Up @@ -1966,6 +1974,7 @@ static int
_validate_outer(SRE_CODE *code, SRE_CODE *end, PatternObject *self)
{
if (self->groups < 0 || (size_t)self->groups > SRE_MAXGROUPS ||
self->repeat_count < 0 ||
code >= end || end[-1] != SRE_OP_SUCCESS)
FAIL;
return _validate_inner(code, end-1, self);
Expand Down