@@ -3203,7 +3203,8 @@ static PyObject *
32033203dec_as_long (PyObject * dec , PyObject * context , int round )
32043204{
32053205 PyLongObject * pylong ;
3206- size_t maxsize , n ;
3206+ digit * ob_digit ;
3207+ size_t n ;
32073208 Py_ssize_t i ;
32083209 mpd_t * x ;
32093210 mpd_context_t workctx ;
@@ -3234,32 +3235,33 @@ dec_as_long(PyObject *dec, PyObject *context, int round)
32343235 return NULL ;
32353236 }
32363237
3237- maxsize = mpd_sizeinbase (x , PyLong_BASE );
3238- if (maxsize > PY_SSIZE_T_MAX ) {
3239- mpd_del (x );
3240- PyErr_NoMemory ();
3241- return NULL ;
3242- }
3243- pylong = _PyLong_New (maxsize );
3244- if (pylong == NULL ) {
3245- mpd_del (x );
3246- return NULL ;
3247- }
3248-
32493238 status = 0 ;
3239+ ob_digit = NULL ;
32503240#if PYLONG_BITS_IN_DIGIT == 30
3251- n = mpd_qexport_u32 (pylong -> ob_digit , maxsize , PyLong_BASE , x , & status );
3241+ n = mpd_qexport_u32 (& ob_digit , 0 , PyLong_BASE , x , & status );
32523242#elif PYLONG_BITS_IN_DIGIT == 15
3253- n = mpd_qexport_u16 (pylong -> ob_digit , maxsize , PyLong_BASE , x , & status );
3243+ n = mpd_qexport_u16 (& ob_digit , 0 , PyLong_BASE , x , & status );
32543244#else
3255- #error "PYLONG_BITS_IN_DIGIT should be 15 or 30"
3245+ #error "PYLONG_BITS_IN_DIGIT should be 15 or 30"
32563246#endif
3257- if (dec_addstatus (context , status )) {
3258- Py_DECREF ((PyObject * ) pylong );
3247+
3248+ if (n == SIZE_MAX ) {
3249+ PyErr_NoMemory ();
3250+ mpd_del (x );
3251+ return NULL ;
3252+ }
3253+
3254+ assert (n > 0 );
3255+ pylong = _PyLong_New (n );
3256+ if (pylong == NULL ) {
3257+ mpd_free (ob_digit );
32593258 mpd_del (x );
32603259 return NULL ;
32613260 }
32623261
3262+ memcpy (pylong -> ob_digit , ob_digit , n * sizeof (digit ));
3263+ mpd_free (ob_digit );
3264+
32633265 i = n ;
32643266 while ((i > 0 ) && (pylong -> ob_digit [i - 1 ] == 0 )) {
32653267 i -- ;
0 commit comments