@@ -633,9 +633,15 @@ static Py_ssize_t PyArg_ParseTupleAndKeywordsFirstN(
633
633
va_list varargs ;
634
634
va_start (varargs , n );
635
635
636
+ Py_ssize_t return_value = -1 ;
636
637
Py_ssize_t nkwargs = kwargs ? PyObject_Length (kwargs ) : 0 ;
637
638
Py_ssize_t n_actual_args = n - nkwargs ;
638
639
640
+ if (PyTuple_GET_SIZE (args ) < n_actual_args ) {
641
+ PyErr_SetString (PyExc_TypeError , "Not enough arguments" );
642
+ goto exit ;
643
+ }
644
+
639
645
PyObject * actual_args = PyTuple_GetSlice (args , 0 , n_actual_args );
640
646
if (!actual_args ) {
641
647
PyErr_NoMemory ();
@@ -646,10 +652,11 @@ static Py_ssize_t PyArg_ParseTupleAndKeywordsFirstN(
646
652
actual_args , kwargs , format , keywords , varargs )) {
647
653
}
648
654
Py_DECREF (actual_args );
655
+ return_value = n_actual_args ;
649
656
650
657
exit :
651
658
va_end (varargs );
652
- return n_actual_args ;
659
+ return return_value ;
653
660
}
654
661
655
662
static bool PopFillPadding (PyObject * kwargs )
@@ -907,21 +914,22 @@ static PyObject* CompiledFormat_pack_into(
907
914
PyObject * kwargs )
908
915
{
909
916
PyObject * return_value = NULL ;
910
-
911
- bool fill_padding = PopFillPadding (kwargs );
912
-
913
917
Py_buffer buffer = {NULL , NULL };
914
918
Py_ssize_t offset = 0 ;
915
919
920
+ bool fill_padding = PopFillPadding (kwargs );
921
+ Py_ssize_t n_args = PyTuple_GET_SIZE (args );
922
+ PyObject * * data = PySequence_Fast_ITEMS (args );
923
+
916
924
static char * _keywords [] = {"buf" , "offset" , NULL };
917
925
// custom (and vague) error message as all other 'pack_into'
918
926
// versions are processed by this function. Using the default
919
927
// error message would give bad information to the user
920
928
Py_ssize_t n_args_parsed = PyArg_ParseTupleAndKeywordsFirstN (
921
929
args , kwargs , "y*n:pack_into" , _keywords , 2 , & buffer , & offset );
922
-
923
- Py_ssize_t n_args = PyTuple_GET_SIZE ( args ) ;
924
- PyObject * * data = PySequence_Fast_ITEMS ( args );
930
+ if ( n_args_parsed < 0 ) {
931
+ goto exit ;
932
+ }
925
933
926
934
return_value = CompiledFormat_pack_into_raw (
927
935
self -> compiled_fmt ,
@@ -931,6 +939,7 @@ static PyObject* CompiledFormat_pack_into(
931
939
n_args - n_args_parsed ,
932
940
fill_padding );
933
941
942
+ exit :
934
943
if (buffer .obj ) {
935
944
PyBuffer_Release (& buffer );
936
945
}
@@ -1325,13 +1334,15 @@ static PyObject* pack(PyObject* module, PyObject* args, PyObject* kwargs)
1325
1334
{
1326
1335
PyObject * return_value = NULL ;
1327
1336
const char * fmt = NULL ;
1337
+ PyCompiledFormatObject self ;
1338
+ memset (& self , 0 , sizeof (self ));
1328
1339
1329
1340
static char * _keywords [] = {"fmt" , NULL };
1330
1341
Py_ssize_t n_args_parsed = PyArg_ParseTupleAndKeywordsFirstN (
1331
1342
args , kwargs , "s:pack" , _keywords , 1 , & fmt );
1332
-
1333
- PyCompiledFormatObject self ;
1334
- memset ( & self , 0 , sizeof ( self ));
1343
+ if ( n_args_parsed < 0 ) {
1344
+ goto exit ;
1345
+ }
1335
1346
1336
1347
if (CompiledFormat___init___impl (& self , fmt )) {
1337
1348
// CompiledFormat___init___impl has set the exception
@@ -1363,15 +1374,16 @@ static PyObject* pack_into(PyObject* module, PyObject* args, PyObject* kwargs)
1363
1374
Py_buffer buffer = {NULL , NULL };
1364
1375
Py_ssize_t offset = 0 ;
1365
1376
const char * fmt = NULL ;
1366
-
1377
+ PyCompiledFormatObject self ;
1378
+ memset (& self , 0 , sizeof (self ));
1367
1379
bool fill_padding = PopFillPadding (kwargs );
1368
1380
1369
1381
static char * _keywords [] = {"fmt" , "buf" , "offset" , NULL };
1370
1382
Py_ssize_t n_args_parsed = PyArg_ParseTupleAndKeywordsFirstN (
1371
1383
args , kwargs , "sy*n:pack_into" , _keywords , 3 , & fmt , & buffer , & offset );
1372
-
1373
- PyCompiledFormatObject self ;
1374
- memset ( & self , 0 , sizeof ( self ));
1384
+ if ( n_args_parsed < 0 ) {
1385
+ goto exit ;
1386
+ }
1375
1387
1376
1388
if (CompiledFormat___init___impl (& self , fmt )) {
1377
1389
// CompiledFormat___init___impl has set the exception
0 commit comments