@@ -357,6 +357,11 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
357357
358358 memset (state , 0 , sizeof (SRE_STATE ));
359359
360+ state -> mark = PyMem_New (void * , pattern -> groups * 2 );
361+ if (!state -> mark ) {
362+ PyErr_NoMemory ();
363+ goto err ;
364+ }
360365 state -> lastmark = -1 ;
361366 state -> lastindex = -1 ;
362367
@@ -409,6 +414,8 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
409414
410415 return string ;
411416 err :
417+ PyMem_Del (state -> mark );
418+ state -> mark = NULL ;
412419 if (state -> buffer .buf )
413420 PyBuffer_Release (& state -> buffer );
414421 return NULL ;
@@ -421,6 +428,8 @@ state_fini(SRE_STATE* state)
421428 PyBuffer_Release (& state -> buffer );
422429 Py_XDECREF (state -> string );
423430 data_stack_dealloc (state );
431+ PyMem_Del (state -> mark );
432+ state -> mark = NULL ;
424433}
425434
426435/* calculate offset from start of string */
@@ -560,6 +569,7 @@ pattern_match(PatternObject *self, PyObject *args, PyObject *kwargs)
560569 PyObject * pattern = NULL ;
561570 SRE_STATE state ;
562571 Py_ssize_t status ;
572+ PyObject * match ;
563573
564574 if (!PyArg_ParseTupleAndKeywords (args , kwargs ,
565575 "|Onn$O:match" , _keywords ,
@@ -579,19 +589,22 @@ pattern_match(PatternObject *self, PyObject *args, PyObject *kwargs)
579589 status = sre_match (& state , PatternObject_GetCode (self ), 0 );
580590
581591 TRACE (("|%p|%p|END\n" , PatternObject_GetCode (self ), state .ptr ));
582- if (PyErr_Occurred ())
592+ if (PyErr_Occurred ()) {
593+ state_fini (& state );
583594 return NULL ;
595+ }
584596
597+ match = pattern_new_match (self , & state , status );
585598 state_fini (& state );
586-
587- return (PyObject * )pattern_new_match (self , & state , status );
599+ return match ;
588600}
589601
590602static PyObject *
591603pattern_fullmatch (PatternObject * self , PyObject * args , PyObject * kw )
592604{
593605 SRE_STATE state ;
594606 Py_ssize_t status ;
607+ PyObject * match ;
595608
596609 PyObject * string = NULL , * string2 = NULL ;
597610 Py_ssize_t start = 0 ;
@@ -616,19 +629,22 @@ pattern_fullmatch(PatternObject* self, PyObject* args, PyObject* kw)
616629 status = sre_match (& state , PatternObject_GetCode (self ), 1 );
617630
618631 TRACE (("|%p|%p|END\n" , PatternObject_GetCode (self ), state .ptr ));
619- if (PyErr_Occurred ())
632+ if (PyErr_Occurred ()) {
633+ state_fini (& state );
620634 return NULL ;
635+ }
621636
637+ match = pattern_new_match (self , & state , status );
622638 state_fini (& state );
623-
624- return pattern_new_match (self , & state , status );
639+ return match ;
625640}
626641
627642static PyObject *
628643pattern_search (PatternObject * self , PyObject * args , PyObject * kw )
629644{
630645 SRE_STATE state ;
631646 Py_ssize_t status ;
647+ PyObject * match ;
632648
633649 PyObject * string = NULL , * string2 = NULL ;
634650 Py_ssize_t start = 0 ;
@@ -652,12 +668,14 @@ pattern_search(PatternObject* self, PyObject* args, PyObject* kw)
652668
653669 TRACE (("|%p|%p|END\n" , PatternObject_GetCode (self ), state .ptr ));
654670
655- state_fini (& state );
656-
657- if (PyErr_Occurred ())
671+ if (PyErr_Occurred ()) {
672+ state_fini (& state );
658673 return NULL ;
674+ }
659675
660- return pattern_new_match (self , & state , status );
676+ match = pattern_new_match (self , & state , status );
677+ state_fini (& state );
678+ return match ;
661679}
662680
663681static PyObject *
@@ -1417,7 +1435,7 @@ _compile(PyObject* self_, PyObject* args)
14171435 PyObject * groupindex = NULL ;
14181436 PyObject * indexgroup = NULL ;
14191437
1420- if (!PyArg_ParseTuple (args , "OiO!| nOO" , & pattern , & flags ,
1438+ if (!PyArg_ParseTuple (args , "OiO!nOO" , & pattern , & flags ,
14211439 & PyList_Type , & code , & groups ,
14221440 & groupindex , & indexgroup ))
14231441 return NULL ;
@@ -1933,10 +1951,9 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
19331951static int
19341952_validate_outer (SRE_CODE * code , SRE_CODE * end , Py_ssize_t groups )
19351953{
1936- if (groups < 0 || groups > 100 || code >= end || end [-1 ] != SRE_OP_SUCCESS )
1954+ if (groups < 0 || (size_t )groups > SRE_MAXGROUPS ||
1955+ code >= end || end [-1 ] != SRE_OP_SUCCESS )
19371956 FAIL ;
1938- if (groups == 0 ) /* fix for simplejson */
1939- groups = 100 ; /* 100 groups should always be safe */
19401957 return _validate_inner (code , end - 1 , groups );
19411958}
19421959
@@ -2747,6 +2764,12 @@ PyMODINIT_FUNC PyInit__sre(void)
27472764 Py_DECREF (x );
27482765 }
27492766
2767+ x = PyLong_FromUnsignedLong (SRE_MAXGROUPS );
2768+ if (x ) {
2769+ PyDict_SetItemString (d , "MAXGROUPS" , x );
2770+ Py_DECREF (x );
2771+ }
2772+
27502773 x = PyUnicode_FromString (copyright );
27512774 if (x ) {
27522775 PyDict_SetItemString (d , "copyright" , x );
0 commit comments