@@ -4215,8 +4215,13 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
4215
4215
/* k-ary values. If the exponent is large enough, table is
4216
4216
* precomputed so that table[i] == a**(2*i+1) % c for i in
4217
4217
* range(EXP_TABLE_LEN).
4218
+ * Note: this is uninitialzed stack trash: don't pay to set it to known
4219
+ * values unless it's needed. Instead ensure that num_table_entries is
4220
+ * set to the number of entries actually filled whenever a branch to the
4221
+ * Error or Done labels is possible.
4218
4222
*/
4219
- PyLongObject * table [EXP_TABLE_LEN ] = {0 };
4223
+ PyLongObject * table [EXP_TABLE_LEN ];
4224
+ Py_ssize_t num_table_entries = 0 ;
4220
4225
4221
4226
/* a, b, c = v, w, x */
4222
4227
CHECK_BINOP (v , w );
@@ -4408,10 +4413,14 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
4408
4413
*/
4409
4414
Py_INCREF (a );
4410
4415
table [0 ] = a ;
4416
+ num_table_entries = 1 ;
4411
4417
MULT (a , a , a2 );
4412
4418
/* table[i] == a**(2*i + 1) % c */
4413
- for (i = 1 ; i < EXP_TABLE_LEN ; ++ i )
4419
+ for (i = 1 ; i < EXP_TABLE_LEN ; ++ i ) {
4420
+ table [i ] = NULL ; /* must set to known value for MULT */
4414
4421
MULT (table [i - 1 ], a2 , table [i ]);
4422
+ ++ num_table_entries ; /* incremented iff MULT succeeded */
4423
+ }
4415
4424
Py_CLEAR (a2 );
4416
4425
4417
4426
/* Repeatedly extract the next (no more than) EXP_WINDOW_SIZE bits
@@ -4472,10 +4481,8 @@ long_pow(PyObject *v, PyObject *w, PyObject *x)
4472
4481
Py_CLEAR (z );
4473
4482
/* fall through */
4474
4483
Done :
4475
- if (Py_SIZE (b ) > HUGE_EXP_CUTOFF / PyLong_SHIFT ) {
4476
- for (i = 0 ; i < EXP_TABLE_LEN ; ++ i )
4477
- Py_XDECREF (table [i ]);
4478
- }
4484
+ for (i = 0 ; i < num_table_entries ; ++ i )
4485
+ Py_DECREF (table [i ]);
4479
4486
Py_DECREF (a );
4480
4487
Py_DECREF (b );
4481
4488
Py_XDECREF (c );
0 commit comments