8000 bpo-23689: re module, fix memory leak when a match is terminated by a signal or memory allocation failure · Pull Request #32283 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-23689: re module, fix memory leak when a match is terminated by a signal or memory allocation failure #32283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from Apr 3, 2022
Prev Previous commit
Next Next commit
8. support code in sre_lib.h
  • Loading branch information
wjssz committed Apr 3, 2022
commit 1560a0cc5977bd12fbb8bae4e7a2210fa3d4073d
31 changes: 15 additions & 16 deletions Modules/sre_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1032,16 +1032,14 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
case SRE_OP_REPEAT:
/* create repeat context. all the hard work is done
by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */
/* <REPEAT> <skip> <1=min> <2=max> item <UNTIL> tail */
TRACE(("|%p|%p|REPEAT %d %d\n", ctx->pattern, ctx->ptr,
ctx->pattern[1], ctx->pattern[2]));
/* <REPEAT> <skip> <1=min> <2=max>
<3=repeat_index> item <UNTIL> tail */
TRACE(("|%p|%p|REPEAT %d %d %d\n", ctx->pattern, ctx->ptr,
ctx->pattern[1], ctx->pattern[2], ctx->pattern[3]));

/* install repeat context */
ctx->u.rep = &state->repeats_array[ctx->pattern[3]];

/* install new repeat context */
ctx->u.rep = (SRE_REPEAT*) PyObject_Malloc(sizeof(*ctx->u.rep));
if (!ctx->u.rep) {
PyErr_NoMemory();
RETURN_FAILURE;
}
ctx->u.rep->count = -1;
ctx->u.rep->pattern = ctx->pattern;
ctx->u.rep->prev = state->repeat;
Expand All @@ -1051,7 +1049,6 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
state->ptr = ctx->ptr;
DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0]);
state->repeat = ctx->u.rep->prev;
PyObject_Free(ctx->u.rep);

if (ret) {
RETURN_ON_ERROR(ret);
Expand All @@ -1061,7 +1058,8 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)

case SRE_OP_MAX_UNTIL:
/* maximizing repeat */
/* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */
/* <REPEAT> <skip> <1=min> <2=max>
<3=repeat_index> item <MAX_UNTIL> tail */

/* FIXME: we probably need to deal with zero-width
matches in here... */
Expand All @@ -1081,7 +1079,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
/* not enough matches */
ctx->u.rep->count = ctx->count;
DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
ctx->u.rep->pattern+3);
ctx->u.rep->pattern+4);
if (ret) {
RETURN_ON_ERROR(ret);
RETURN_SUCCESS;
Expand All @@ -1103,7 +1101,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
DATA_PUSH(&ctx->u.rep->last_ptr);
ctx->u.rep->last_ptr = state->ptr;
DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
ctx->u.rep->pattern+3);
ctx->u.rep->pattern+4);
DATA_POP(&ctx->u.rep->last_ptr);
if (ret) {
MARK_POP_DISCARD(ctx->lastmark);
Expand All @@ -1128,7 +1126,8 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)

case SRE_OP_MIN_UNTIL:
/* minimizing repeat */
/* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */
/* <REPEAT> <skip> <1=min> <2=max>
<3=repeat_index> item <MIN_UNTIL> tail */

ctx->u.rep = state->repeat;
if (!ctx->u.rep)
Expand All @@ -1145,7 +1144,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
/* not enough matches */
ctx->u.rep->count = ctx->count;
DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
ctx->u.rep->pattern+3);
ctx->u.rep->pattern+4);
if (ret) {
RETURN_ON_ERROR(ret);
RETURN_SUCCESS;
Expand Down Expand Up @@ -1188,7 +1187,7 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
DATA_PUSH(&ctx->u.rep->last_ptr);
ctx->u.rep->last_ptr = state->ptr;
DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
ctx->u.rep->pattern+3);
ctx->u.rep->pattern+4);
DATA_POP(&ctx->u.rep->last_ptr);
if (ret) {
RETURN_ON_ERROR(ret);
Expand Down
0