@@ -203,25 +203,28 @@ PyLzma_Free(void *opaque, void *ptr)
203203      to be strictly correct, we need to define two separate converters. 
204204 */ 
205205
206- #define  INT_TYPE_CONVERTER_FUNC (TYPE , FUNCNAME ) \
207-     static int \
208-     FUNCNAME(PyObject *obj, void *ptr) \
209-     { \
210-         unsigned long long val; \
211-         \
212-         val = PyLong_AsUnsignedLongLong(obj); \
213-         if (PyErr_Occurred()) \
214-             return 0; \
215-         if ((unsigned long long)(TYPE)val != val) { \
216-             PyErr_SetString(PyExc_OverflowError, \
217-                             "Value too large for " #TYPE " type"); \
218-             return 0; \
219-         } \
220-         *(TYPE *)ptr = (TYPE)val; \
221-         return 1; \
222-     }
206+ #define  INT_TYPE_CONVERTER_FUNC (TYPE , FUNCNAME )                     \
207+ static int                                                          \
208+ FUNCNAME(PyObject *obj, void *ptr)                                  \
209+ {                                                                   \
210+     Py_ssize_t bytes = PyLong_AsNativeBytes(obj, ptr, sizeof(TYPE), \
211+             Py_ASNATIVEBYTES_NATIVE_ENDIAN |                        \
212+             Py_ASNATIVEBYTES_ALLOW_INDEX |                          \
213+             Py_ASNATIVEBYTES_REJECT_NEGATIVE |                      \
214+             Py_ASNATIVEBYTES_UNSIGNED_BUFFER);                      \
215+     if (bytes < 0) {                                                \
216+         return 0;                                                   \
217+     }                                                               \
218+     if ((size_t)bytes > sizeof(TYPE)) {                             \
219+         PyErr_SetString(PyExc_OverflowError,                        \
220+                         "Python int too large for C "#TYPE);        \
221+         return 0;                                                   \
222+     }                                                               \
223+     return 1;                                                       \
224+ }
223225
224226INT_TYPE_CONVERTER_FUNC (uint32_t , uint32_converter )
227+ INT_TYPE_CONVERTER_FUNC (uint64_t , uint64_converter )
225228INT_TYPE_CONVERTER_FUNC (lzma_vli , lzma_vli_converter )
226229INT_TYPE_CONVERTER_FUNC (lzma_mode , lzma_mode_converter )
227230INT_TYPE_CONVERTER_FUNC (lzma_match_finder , lzma_mf_converter )
@@ -355,11 +358,12 @@ lzma_filter_converter(_lzma_state *state, PyObject *spec, void *ptr)
355358                        "Filter specifier must have an \"id\" entry" );
356359        return  0 ;
357360    }
358-     f -> id   =   PyLong_AsUnsignedLongLong ( id_obj ) ;
359-     Py_DECREF ( id_obj ); 
360-     if  ( PyErr_Occurred ()) { 
361+     lzma_vli   id ;
362+     if  (! lzma_vli_converter ( id_obj ,  & id )) { 
363+          Py_DECREF ( id_obj ); 
361364        return  0 ;
362365    }
366+     f -> id  =  id ;
363367
364368    switch  (f -> id ) {
365369        case  LZMA_FILTER_LZMA1 :
@@ -1221,8 +1225,7 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
12211225                            "Cannot specify memory limit with FORMAT_RAW" );
12221226            return  NULL ;
12231227        }
1224-         memlimit_  =  PyLong_AsUnsignedLongLong (memlimit );
1225-         if  (PyErr_Occurred ()) {
1228+         if  (!uint64_converter (memlimit , & memlimit_ )) {
12261229            return  NULL ;
12271230        }
12281231    }
0 commit comments