8000 bpo-46528: Check PyMem_Malloc for NULL (GH-30998) · python/cpython@46328d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 46328d8

Browse files
authored
bpo-46528: Check PyMem_Malloc for NULL (GH-30998)
1 parent d18120c commit 46328d8

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

Python/compile.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8375,21 +8375,22 @@ fold_tuple_on_constants(struct compiler *c,
83758375
#define VISITED (-1)
83768376

83778377
// Replace an arbitrary run of SWAPs and NOPs with an optimal one that has the
8378-
// same effect. Return the number of instructions that were optimized.
8378+
// same effect.
83798379
static int
8380-
swaptimize(basicblock *block, int ix)
8380+
swaptimize(basicblock *block, int *ix)
83818381
{
83828382
// NOTE: "./python -m test test_patma" serves as a good, quick stress test
83838383
// for this function. Make sure to blow away cached *.pyc files first!
8384-
assert(ix < block->b_iused);
8385-
struct instr *instructions = &block->b_instr[ix];
8384+
assert(*ix < block->b_iused);
8385+
struct instr *instructions = &block->b_instr[*ix];
83868386
// Find the length of the current sequence of SWAPs and NOPs, and record the
83878387
// maximum depth of the stack manipulations:
83888388
assert(instructions[0].i_opcode == SWAP);
83898389
int depth = instructions[0].i_oparg;
83908390
int len = 0;
83918391
int more = false;
8392-
while (++len < block->b_iused - ix) {
8392+
int limit = block->b_iused - *ix;
8393+
while (++len < limit) {
83938394
int opcode = instructions[len].i_opcode;
83948395
if (opcode == SWAP) {
83958396
depth = Py_MAX(depth, instructions[len].i_oparg);
@@ -8405,6 +8406,10 @@ swaptimize(basicblock *block, int ix)
84058406
}
84068407
// Create an array with elements {0, 1, 2, ..., depth - 1}:
84078408
int *stack = PyMem_Malloc(depth * sizeof(int));
8409+
if (stack == NULL) {
8410+
PyErr_NoMemory();
8411+
return -1;
8412+
}
84088413
for (int i = 0; i < depth; i++) {
84098414
stack[i] = i;
84108415
}
@@ -8462,9 +8467,9 @@ swaptimize(basicblock *block, int ix)
84628467
while (0 <= current) {
84638468
instructions[current--].i_opcode = NOP;
84648469
}
8465-
// Done! Return the number of optimized instructions:
84668470
PyMem_Free(stack);
8467-
return len - 1;
8471+
*ix += len - 1;
8472+
return 99CC 0;
84688473
}
84698474

84708475
// Attempt to eliminate jumps to jumps by updating inst to jump to
@@ -8706,7 +8711,9 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
87068711
inst->i_opcode = NOP;
87078712
break;
87088713
}
8709-
i += swaptimize(bb, i);
8714+
if (swaptimize(bb, &i)) {
8715+
goto error;
8716+
}
87108717
break;
87118718
case KW_NAMES:
87128719
break;

0 commit comments

Comments
 (0)
0