Skip to content

Commit 59664c0

Browse files
picnixzsrinivasreddy
authored andcommitted
pythongh-111178: fix UBSan failures in Modules/_sre/sre.c (pythonGH-128250)
fix UBSan failures for `PatternObject`, `MatchObject`, `TemplateObject`, `ScannerObject`
1 parent aca74a2 commit 59664c0

File tree

1 file changed

+56
-35
lines changed

1 file changed

+56
-35
lines changed

Modules/_sre/sre.c

+56-35
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ static struct PyModuleDef sremodule;
395395
static PyObject*pattern_new_match(_sremodulestate *, PatternObject*, SRE_STATE*, Py_ssize_t);
396396
static PyObject *pattern_scanner(_sremodulestate *, PatternObject *, PyObject *, Py_ssize_t, Py_ssize_t);
397397

398+
#define _PatternObject_CAST(op) ((PatternObject *)(op))
399+
#define _MatchObject_CAST(op) ((MatchObject *)(op))
400+
#define _TemplateObject_CAST(op) ((TemplateObject *)(op))
401+
#define _ScannerObject_CAST(op) ((ScannerObject *)(op))
402+
398403
/*[clinic input]
399404
module _sre
400405
class _sre.SRE_Pattern "PatternObject *" "get_sre_module_state_by_class(tp)->Pattern_Type"
@@ -699,8 +704,9 @@ pattern_error(Py_ssize_t status)
699704
}
700705

701706
static int
702-
pattern_traverse(PatternObject *self, visitproc visit, void *arg)
707+
pattern_traverse(PyObject *op, visitproc visit, void *arg)
703708
{
709+
PatternObject *self = _PatternObject_CAST(op);
704710
Py_VISIT(Py_TYPE(self));
705711
Py_VISIT(self->groupindex);
706712
Py_VISIT(self->indexgroup);
@@ -712,8 +718,9 @@ pattern_traverse(PatternObject *self, visitproc visit, void *arg)
712718
}
713719

714720
static int
715-
pattern_clear(PatternObject *self)
721+
pattern_clear(PyObject *op)
716722
{
723+
PatternObject *self = _PatternObject_CAST(op);
717724
Py_CLEAR(self->groupindex);
718725
Py_CLEAR(self->indexgroup);
719726
Py_CLEAR(self->pattern);
@@ -724,13 +731,13 @@ pattern_clear(PatternObject *self)
724731
}
725732

726733
static void
727-
pattern_dealloc(PatternObject* self)
734+
pattern_dealloc(PyObject *self)
728735
{
729736
PyTypeObject *tp = Py_TYPE(self);
730-
731737
PyObject_GC_UnTrack(self);
732-
if (self->weakreflist != NULL) {
733-
PyObject_ClearWeakRefs((PyObject *) self);
738+
PatternObject *obj = _PatternObject_CAST(self);
739+
if (obj->weakreflist != NULL) {
740+
PyObject_ClearWeakRefs(self);
734741
}
735742
(void)pattern_clear(self);
736743
tp->tp_free(self);
@@ -1497,7 +1504,7 @@ _sre_SRE_Pattern__fail_after_impl(PatternObject *self, int count,
14971504
#endif /* Py_DEBUG */
14981505

14991506
static PyObject *
1500-
pattern_repr(PatternObject *obj)
1507+
pattern_repr(PyObject *self)
15011508
{
15021509
static const struct {
15031510
const char *name;
@@ -1512,6 +1519,8 @@ pattern_repr(PatternObject *obj)
15121519
{"re.DEBUG", SRE_FLAG_DEBUG},
15131520
{"re.ASCII", SRE_FLAG_ASCII},
15141521
};
1522+
1523+
PatternObject *obj = _PatternObject_CAST(self);
15151524
PyObject *result = NULL;
15161525
PyObject *flag_items;
15171526
size_t i;
@@ -1579,8 +1588,9 @@ PyDoc_STRVAR(pattern_doc, "Compiled regular expression object.");
15791588

15801589
/* PatternObject's 'groupindex' method. */
15811590
static PyObject *
1582-
pattern_groupindex(PatternObject *self, void *Py_UNUSED(ignored))
1591+
pattern_groupindex(PyObject *op, void *Py_UNUSED(ignored))
15831592
{
1593+
PatternObject *self = _PatternObject_CAST(op);
15841594
if (self->groupindex == NULL)
15851595
return PyDict_New();
15861596
return PyDictProxy_New(self->groupindex);
@@ -2245,8 +2255,9 @@ _validate(PatternObject *self)
22452255
/* match methods */
22462256

22472257
static int
2248-
match_traverse(MatchObject *self, visitproc visit, void *arg)
2258+
match_traverse(PyObject *op, visitproc visit, void *arg)
22492259
{
2260+
MatchObject *self = _MatchObject_CAST(op);
22502261
Py_VISIT(Py_TYPE(self));
22512262
Py_VISIT(self->string);
22522263
Py_VISIT(self->regs);
@@ -2255,19 +2266,19 @@ match_traverse(MatchObject *self, visitproc visit, void *arg)
22552266
}
22562267

22572268
static int
2258-
match_clear(MatchObject *self)
2269+
match_clear(PyObject *op)
22592270
{
2271+
MatchObject *self = _MatchObject_CAST(op);
22602272
Py_CLEAR(self->string);
22612273
Py_CLEAR(self->regs);
22622274
Py_CLEAR(self->pattern);
22632275
return 0;
22642276
}
22652277

22662278
static void
2267-
match_dealloc(MatchObject* self)
2279+
match_dealloc(PyObject *self)
22682280
{
22692281
PyTypeObject *tp = Py_TYPE(self);
2270-
22712282
PyObject_GC_UnTrack(self);
22722283
(void)match_clear(self);
22732284
tp->tp_free(self);
@@ -2376,8 +2387,9 @@ _sre_SRE_Match_expand_impl(MatchObject *self, PyObject *template)
23762387
}
23772388

23782389
static PyObject*
2379-
match_group(MatchObject* self, PyObject* args)
2390+
match_group(PyObject *op, PyObject* args)
23802391
{
2392+
MatchObject *self = _MatchObject_CAST(op);
23812393
PyObject* result;
23822394
Py_ssize_t i, size;
23832395

@@ -2411,8 +2423,9 @@ match_group(MatchObject* self, PyObject* args)
24112423
}
24122424

24132425
static PyObject*
2414-
match_getitem(MatchObject* self, PyObject* name)
2426+
match_getitem(PyObject *op, PyObject* name)
24152427
{
2428+
MatchObject *self = _MatchObject_CAST(op);
24162429
return match_getslice(self, name, Py_None);
24172430
}
24182431

@@ -2654,16 +2667,18 @@ PyDoc_STRVAR(match_group_doc,
26542667
For 0 returns the entire match.");
26552668

26562669
static PyObject *
2657-
match_lastindex_get(MatchObject *self, void *Py_UNUSED(ignored))
2670+
match_lastindex_get(PyObject *op, void *Py_UNUSED(ignored))
26582671
{
2672+
MatchObject *self = _MatchObject_CAST(op);
26592673
if (self->lastindex >= 0)
26602674
return PyLong_FromSsize_t(self->lastindex);
26612675
Py_RETURN_NONE;
26622676
}
26632677

26642678
static PyObject *
2665-
match_lastgroup_get(MatchObject *self, void *Py_UNUSED(ignored))
2679+
match_lastgroup_get(PyObject *op, void *Py_UNUSED(ignored))
26662680
{
2681+
MatchObject *self = _MatchObject_CAST(op);
26672682
if (self->pattern->indexgroup &&
26682683
self->lastindex >= 0 &&
26692684
self->lastindex < PyTuple_GET_SIZE(self->pattern->indexgroup))
@@ -2676,8 +2691,9 @@ match_lastgroup_get(MatchObject *self, void *Py_UNUSED(ignored))
26762691
}
26772692

26782693
static PyObject *
2679-
match_regs_get(MatchObject *self, void *Py_UNUSED(ignored))
2694+
match_regs_get(PyObject *op, void *Py_UNUSED(ignored))
26802695
{
2696+
MatchObject *self = _MatchObject_CAST(op);
26812697
if (self->regs) {
26822698
return Py_NewRef(self->regs);
26832699
} else
@@ -2780,27 +2796,29 @@ pattern_new_match(_sremodulestate* module_state,
27802796
/* scanner methods (experimental) */
27812797

27822798
static int
2783-
scanner_traverse(ScannerObject *self, visitproc visit, void *arg)
2799+
scanner_traverse(PyObject *op, visitproc visit, void *arg)
27842800
{
2801+
ScannerObject *self = _ScannerObject_CAST(op);
27852802
Py_VISIT(Py_TYPE(self));
27862803
Py_VISIT(self->pattern);
27872804
return 0;
27882805
}
27892806

27902807
static int
2791-
scanner_clear(ScannerObject *self)
2808+
scanner_clear(PyObject *op)
27922809
{
2810+
ScannerObject *self = _ScannerObject_CAST(op);
27932811
Py_CLEAR(self->pattern);
27942812
return 0;
27952813
}
27962814

27972815
static void
2798-
scanner_dealloc(ScannerObject* self)
2816+
scanner_dealloc(PyObject *self)
27992817
{
28002818
PyTypeObject *tp = Py_TYPE(self);
2801-
28022819
PyObject_GC_UnTrack(self);
2803-
state_fini(&self->state);
2820+
ScannerObject *scanner = _ScannerObject_CAST(self);
2821+
state_fini(&scanner->state);
28042822
(void)scanner_clear(self);
28052823
tp->tp_free(self);
28062824
Py_DECREF(tp);
@@ -2957,8 +2975,9 @@ pattern_scanner(_sremodulestate *module_state,
29572975
/* template methods */
29582976

29592977
static int
2960-
template_traverse(TemplateObject *self, visitproc visit, void *arg)
2978+
template_traverse(PyObject *op, visitproc visit, void *arg)
29612979
{
2980+
TemplateObject *self = _TemplateObject_CAST(op);
29622981
Py_VISIT(Py_TYPE(self));
29632982
Py_VISIT(self->literal);
29642983
for (Py_ssize_t i = 0, n = Py_SIZE(self); i < n; i++) {
@@ -2968,8 +2987,9 @@ template_traverse(TemplateObject *self, visitproc visit, void *arg)
29682987
}
29692988

29702989
static int
2971-
template_clear(TemplateObject *self)
2990+
template_clear(PyObject *op)
29722991
{
2992+
TemplateObject *self = _TemplateObject_CAST(op);
29732993
Py_CLEAR(self->literal);
29742994
for (Py_ssize_t i = 0, n = Py_SIZE(self); i < n; i++) {
29752995
Py_CLEAR(self->items[i].literal);
@@ -2978,10 +2998,9 @@ template_clear(TemplateObject *self)
29782998
}
29792999

29803000
static void
2981-
template_dealloc(TemplateObject *self)
3001+
template_dealloc(PyObject *self)
29823002
{
29833003
PyTypeObject *tp = Py_TYPE(self);
2984-
29853004
PyObject_GC_UnTrack(self);
29863005
(void)template_clear(self);
29873006
tp->tp_free(self);
@@ -3056,8 +3075,10 @@ expand_template(TemplateObject *self, MatchObject *match)
30563075

30573076

30583077
static Py_hash_t
3059-
pattern_hash(PatternObject *self)
3078+
pattern_hash(PyObject *op)
30603079
{
3080+
PatternObject *self = _PatternObject_CAST(op);
3081+
30613082
Py_hash_t hash, hash2;
30623083

30633084
hash = PyObject_Hash(self->pattern);
@@ -3148,7 +3169,7 @@ static PyMethodDef pattern_methods[] = {
31483169
};
31493170

31503171
static PyGetSetDef pattern_getset[] = {
3151-
{"groupindex", (getter)pattern_groupindex, (setter)NULL,
3172+
{"groupindex", pattern_groupindex, NULL,
31523173
"A dictionary mapping group names to group numbers."},
31533174
{NULL} /* Sentinel */
31543175
};
@@ -3166,9 +3187,9 @@ static PyMemberDef pattern_members[] = {
31663187
};
31673188

31683189
static PyType_Slot pattern_slots[] = {
3169-
{Py_tp_dealloc, (destructor)pattern_dealloc},
3170-
{Py_tp_repr, (reprfunc)pattern_repr},
3171-
{Py_tp_hash, (hashfunc)pattern_hash},
3190+
{Py_tp_dealloc, pattern_dealloc},
3191+
{Py_tp_repr, pattern_repr},
3192+
{Py_tp_hash, pattern_hash},
31723193
{Py_tp_doc, (void *)pattern_doc},
31733194
{Py_tp_richcompare, pattern_richcompare},
31743195
{Py_tp_methods, pattern_methods},
@@ -3189,7 +3210,7 @@ static PyType_Spec pattern_spec = {
31893210
};
31903211

31913212
static PyMethodDef match_methods[] = {
3192-
{"group", (PyCFunction) match_group, METH_VARARGS, match_group_doc},
3213+
{"group", match_group, METH_VARARGS, match_group_doc},
31933214
_SRE_SRE_MATCH_START_METHODDEF
31943215
_SRE_SRE_MATCH_END_METHODDEF
31953216
_SRE_SRE_MATCH_SPAN_METHODDEF
@@ -3204,11 +3225,11 @@ static PyMethodDef match_methods[] = {
32043225
};
32053226

32063227
static PyGetSetDef match_getset[] = {
3207-
{"lastindex", (getter)match_lastindex_get, (setter)NULL,
3228+
{"lastindex", match_lastindex_get, NULL,
32083229
"The integer index of the last matched capturing group."},
3209-
{"lastgroup", (getter)match_lastgroup_get, (setter)NULL,
3230+
{"lastgroup", match_lastgroup_get, NULL,
32103231
"The name of the last matched capturing group."},
3211-
{"regs", (getter)match_regs_get, (setter)NULL},
3232+
{"regs", match_regs_get, NULL, NULL},
32123233
{NULL}
32133234
};
32143235

0 commit comments

Comments
 (0)