@@ -203,25 +203,28 @@ PyLzma_Free(void *opaque, void *ptr)
203
203
to be strictly correct, we need to define two separate converters.
204
204
*/
205
205
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
+ }
223
225
224
226
INT_TYPE_CONVERTER_FUNC (uint32_t , uint32_converter )
227
+ INT_TYPE_CONVERTER_FUNC (uint64_t , uint64_converter )
225
228
INT_TYPE_CONVERTER_FUNC (lzma_vli , lzma_vli_converter )
226
229
INT_TYPE_CONVERTER_FUNC (lzma_mode , lzma_mode_converter )
227
230
INT_TYPE_CONVERTER_FUNC (lzma_match_finder , lzma_mf_converter )
@@ -355,11 +358,13 @@ lzma_filter_converter(_lzma_state *state, PyObject *spec, void *ptr)
355
358
"Filter specifier must have an \"id\" entry" );
356
359
return 0 ;
357
360
}
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 );
361
364
return 0 ;
362
365
}
366
+ Py_DECREF (id_obj );
367
+ f -> id = id ;
363
368
364
369
switch (f -> id ) {
365
370
case LZMA_FILTER_LZMA1 :
@@ -1221,8 +1226,7 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
1221
1226
"Cannot specify memory limit with FORMAT_RAW" );
1222
1227
return NULL ;
1223
1228
}
1224
- memlimit_ = PyLong_AsUnsignedLongLong (memlimit );
1225
- if (PyErr_Occurred ()) {
1229
+ if (!uint64_converter (memlimit , & memlimit_ )) {
1226
1230
return NULL ;
1227
1231
}
1228
1232
}
0 commit comments