Skip to content

Commit 1bed6f6

Browse files
committed
fix UBSan failures for PyDecContextObject
1 parent 8b55a0f commit 1bed6f6

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

Modules/_decimal/_decimal.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ typedef struct {
216216
#define MPD(v) (&((PyDecObject *)v)->dec)
217217
#define SdFlagAddr(v) (((PyDecSignalDictObject *)v)->flags)
218218
#define SdFlags(v) (*((PyDecSignalDictObject *)v)->flags)
219-
#define CTX(v) (&((PyDecContextObject *)v)->ctx)
220-
#define CtxCaps(v) (((PyDecContextObject *)v)->capitals)
219+
#define CTX(v) (&_PyDecContextObject_CAST(v)->ctx)
220+
#define CtxCaps(v) (_PyDecContextObject_CAST(v)->capitals)
221221

222222
static inline decimal_state *
223223
get_module_state_from_ctx(PyObject *v)
@@ -1421,24 +1421,26 @@ context_new(PyTypeObject *type,
14211421
}
14221422

14231423
static int
1424-
context_traverse(PyDecContextObject *self, visitproc visit, void *arg)
1424+
context_traverse(PyObject *op, visitproc visit, void *arg)
14251425
{
1426+
PyDecContextObject *self = _PyDecContextObject_CAST(op);
14261427
Py_VISIT(Py_TYPE(self));
14271428
Py_VISIT(self->traps);
14281429
Py_VISIT(self->flags);
14291430
return 0;
14301431
}
14311432

14321433
static int
1433-
context_clear(PyDecContextObject *self)
1434+
context_clear(PyObject *op)
14341435
{
1436+
PyDecContextObject *self = _PyDecContextObject_CAST(op);
14351437
Py_CLEAR(self->traps);
14361438
Py_CLEAR(self->flags);
14371439
return 0;
14381440
}
14391441

14401442
static void
1441-
context_dealloc(PyDecContextObject *self)
1443+
context_dealloc(PyObject *self)
14421444
{
14431445
PyTypeObject *tp = Py_TYPE(self);
14441446
PyObject_GC_UnTrack(self);
@@ -1481,15 +1483,15 @@ context_init(PyObject *self, PyObject *args, PyObject *kwds)
14811483
}
14821484

14831485
static PyObject *
1484-
context_repr(PyDecContextObject *self)
1486+
context_repr(PyObject *self)
14851487
{
14861488
mpd_context_t *ctx;
14871489
char flags[MPD_MAX_SIGNAL_LIST];
14881490
char traps[MPD_MAX_SIGNAL_LIST];
14891491
int n, mem;
14901492

14911493
#ifdef Py_DEBUG
1492-
decimal_state *state = get_module_state_from_ctx((PyObject *)self);
1494+
decimal_state *state = get_module_state_from_ctx(self);
14931495
assert(PyDecContext_Check(state, self));
14941496
#endif
14951497
ctx = CTX(self);
@@ -1509,7 +1511,7 @@ context_repr(PyDecContextObject *self)
15091511
"Context(prec=%zd, rounding=%s, Emin=%zd, Emax=%zd, "
15101512
"capitals=%d, clamp=%d, flags=%s, traps=%s)",
15111513
ctx->prec, mpd_round_string[ctx->round], ctx->emin, ctx->emax,
1512-
self->capitals, ctx->clamp, flags, traps);
1514+
CtxCaps(self), ctx->clamp, flags, traps);
15131515
}
15141516

15151517
static void
@@ -1629,16 +1631,16 @@ context_reduce(PyObject *self, PyObject *Py_UNUSED(dummy))
16291631

16301632
static PyGetSetDef context_getsets [] =
16311633
{
1632-
{ "prec", (getter)context_getprec, (setter)context_setprec, NULL, NULL},
1633-
{ "Emax", (getter)context_getemax, (setter)context_setemax, NULL, NULL},
1634-
{ "Emin", (getter)context_getemin, (setter)context_setemin, NULL, NULL},
1635-
{ "rounding", (getter)context_getround, (setter)context_setround, NULL, NULL},
1636-
{ "capitals", (getter)context_getcapitals, (setter)context_setcapitals, NULL, NULL},
1637-
{ "clamp", (getter)context_getclamp, (setter)context_setclamp, NULL, NULL},
1634+
{ "prec", context_getprec, context_setprec, NULL, NULL},
1635+
{ "Emax", context_getemax, context_setemax, NULL, NULL},
1636+
{ "Emin", context_getemin, context_setemin, NULL, NULL},
1637+
{ "rounding", context_getround, context_setround, NULL, NULL},
1638+
{ "capitals", context_getcapitals, context_setcapitals, NULL, NULL},
1639+
{ "clamp", context_getclamp, context_setclamp, NULL, NULL},
16381640
#ifdef EXTRA_FUNCTIONALITY
1639-
{ "_allcr", (getter)context_getallcr, (setter)context_setallcr, NULL, NULL},
1640-
{ "_traps", (getter)context_gettraps, (setter)context_settraps, NULL, NULL},
1641-
{ "_flags", (getter)context_getstatus, (setter)context_setstatus, NULL, NULL},
1641+
{ "_allcr", context_getallcr, context_setallcr, NULL, NULL},
1642+
{ "_traps", context_gettraps, context_settraps, NULL, NULL},
1643+
{ "_flags", context_getstatus, context_setstatus, NULL, NULL},
16421644
#endif
16431645
{NULL}
16441646
};

0 commit comments

Comments
 (0)