@@ -3675,8 +3675,8 @@ 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 ;
@@ -3685,14 +3685,15 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
36853685 status = 0 ;
36863686 /* mpd_sizeinbase can overestimate size by 1 digit, set it to zero. */
36873687#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 );
3688+ ((uint32_t * )digits )[len - 1 ] = 0 ;
3689+ n = mpd_qexport_u32 ((uint32_t * * )& digits , len , PyLong_BASE , x , & status );
36903690#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 );
3691+ ((uint16_t * )digits )[len - 1 ] = 0 ;
3692+ n = mpd_qexport_u16 ((uint16_t * * )& digits , len , PyLong_BASE , x , & status );
36933693#else
36943694 #error "PYLONG_BITS_IN_DIGIT should be 15 or 30"
36953695#endif
3696+ assert (n == len || n == len - 1 );
36963697
36973698 if (n == SIZE_MAX ) {
36983699 PyErr_NoMemory ();
0 commit comments