Skip to content

Commit 4bb9ad0

Browse files
committed
add asserts & simplify len estimate
1 parent 541441e commit 4bb9ad0

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

Modules/_decimal/_decimal.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,24 +3675,22 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
36753675
return PyLong_FromInt64(val);
36763676
}
36773677

3678-
n = (mpd_sizeinbase(x, 2) + PyLong_SHIFT - 1)/PyLong_SHIFT;
3679-
PyLongWriter *writer = PyLongWriter_Create(mpd_isnegative(x), n, &digits);
3678+
size_t len = mpd_sizeinbase(x, PyLong_BASE);
3679+
PyLongWriter *writer = PyLongWriter_Create(mpd_isnegative(x), len, &digits);
36803680
if (writer == NULL) {
36813681
mpd_del(x);
36823682
return NULL;
36833683
}
36843684

36853685
status = 0;
3686-
/* mpd_sizeinbase can overestimate size by 1 digit, set it to zero. */
36873686
#if PYLONG_BITS_IN_DIGIT == 30
3688-
((uint32_t *)digits)[n - 1] = 0;
3689-
n = mpd_qexport_u32((uint32_t **)&digits, n, PyLong_BASE, x, &status);
3687+
n = mpd_qexport_u32((uint32_t **)&digits, len, PyLong_BASE, x, &status);
36903688
#elif PYLONG_BITS_IN_DIGIT == 15
3691-
((uint16_t *)digits)[n - 1] = 0;
3692-
n = mpd_qexport_u16((uint16_t **)&digits, n, PyLong_BASE, x, &status);
3689+
n = mpd_qexport_u16((uint16_t **)&digits, len, PyLong_BASE, x, &status);
36933690
#else
36943691
#error "PYLONG_BITS_IN_DIGIT should be 15 or 30"
36953692
#endif
3693+
assert(n == len);
36963694

36973695
if (n == SIZE_MAX) {
36983696
PyErr_NoMemory();

0 commit comments

Comments
 (0)