@@ -50,9 +50,9 @@ lastn_const_start(const _Py_CODEUNIT *codestr, Py_ssize_t i, Py_ssize_t n)
5050
5151/* Scans through EXTENDED ARGs, seeking the index of the effective opcode */
5252static Py_ssize_t
53- find_op (const _Py_CODEUNIT * codestr , Py_ssize_t i )
53+ find_op (const _Py_CODEUNIT * codestr , Py_ssize_t codelen , Py_ssize_t i )
5454{
55- while (_Py_OPCODE (codestr [i ]) == EXTENDED_ARG ) {
55+ while (i < codelen && _Py_OPCODE (codestr [i ]) == EXTENDED_ARG ) {
5656 i ++ ;
5757 }
5858 return i ;
@@ -128,8 +128,9 @@ copy_op_arg(_Py_CODEUNIT *codestr, Py_ssize_t i, unsigned char op,
128128 Called with codestr pointing to the first LOAD_CONST.
129129*/
130130static Py_ssize_t
131- fold_tuple_on_constants (_Py_CODEUNIT * codestr , Py_ssize_t c_start ,
132- Py_ssize_t opcode_end , PyObject * consts , int n )
131+ fold_tuple_on_constants (_Py_CODEUNIT * codestr , Py_ssize_t codelen ,
132+ Py_ssize_t c_start , Py_ssize_t opcode_end ,
133+ PyObject * consts , int n )
133134{
134135 /* Pre-conditions */
135136 assert (PyList_CheckExact (consts ));
@@ -142,7 +143,7 @@ fold_tuple_on_constants(_Py_CODEUNIT *codestr, Py_ssize_t c_start,
142143
143144 for (Py_ssize_t i = 0 , pos = c_start ; i < n ; i ++ , pos ++ ) {
144145 assert (pos < opcode_end );
145- pos = find_op (codestr , pos );
146+ pos = find_op (codestr , codelen , pos );
146147 assert (_Py_OPCODE (codestr [pos ]) == LOAD_CONST );
147148
148149 unsigned int arg = get_arg (codestr , pos );
@@ -265,7 +266,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
265266 goto exitError ;
266267 assert (PyList_Check (consts ));
267268
268- for (i = find_op (codestr , 0 ) ; i < codelen ; i = nexti ) {
269+ for (i = find_op (codestr , codelen , 0 ) ; i < codelen ; i = nexti ) {
269270 opcode = _Py_OPCODE (codestr [i ]);
270271 op_start = i ;
271272 while (op_start >= 1 && _Py_OPCODE (codestr [op_start - 1 ]) == EXTENDED_ARG ) {
@@ -303,7 +304,8 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
303304 if (j > 0 && lastlc >= j ) {
304305 h = lastn_const_start (codestr , op_start , j );
305306 if (ISBASICBLOCK (blocks , h , op_start )) {
306- h = fold_tuple_on_constants (codestr , h , i + 1 , consts , j );
307+ h = fold_tuple_on_constants (codestr , codelen ,
308+ h , i + 1 , consts , j );
307309 break ;
308310 }
309311 }
@@ -340,7 +342,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
340342 case JUMP_IF_FALSE_OR_POP :
341343 case JUMP_IF_TRUE_OR_POP :
342344 h = get_arg (codestr , i ) / sizeof (_Py_CODEUNIT );
343- tgt = find_op (codestr , h );
345+ tgt = find_op (codestr , codelen , h );
344346
345347 j = _Py_OPCODE (codestr [tgt ]);
346348 if (CONDITIONAL_JUMP (j )) {
@@ -374,7 +376,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
374376 case JUMP_FORWARD :
375377 case JUMP_ABSOLUTE :
376378 h = GETJUMPTGT (codestr , i );
377- tgt = find_op (codestr , h );
379+ tgt = find_op (codestr , codelen , h );
378380 /* Replace JUMP_* to a RETURN into just a RETURN */
379381 if (UNCONDITIONAL_JUMP (opcode ) &&
380382 _Py_OPCODE (codestr [tgt ]) == RETURN_VALUE ) {
@@ -417,7 +419,7 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
417419 }
418420 if (h > i + 1 ) {
419421 fill_nops (codestr , i + 1 , h );
420- nexti = find_op (codestr , h );
422+ nexti = find_op (codestr , codelen , h );
421423 }
422424 break ;
423425 }
0 commit comments