summaryrefslogtreecommitdiffstats
path: root/Modules/_sre/sre_lib.h
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2022-06-17 08:19:44 (GMT)
committerGitHub <noreply@github.com>2022-06-17 08:19:44 (GMT)
commit4beee0c7b0c2cc78a893dde88fd8e34099dcf877 (patch)
treee3fa8fb0d8260f7c3d16d795c29580386868a5a6 /Modules/_sre/sre_lib.h
parent538f28921f67e36617272faa662375d305d9284c (diff)
downloadcpython-4beee0c7b0c2cc78a893dde88fd8e34099dcf877.zip
cpython-4beee0c7b0c2cc78a893dde88fd8e34099dcf877.tar.gz
cpython-4beee0c7b0c2cc78a893dde88fd8e34099dcf877.tar.bz2
gh-91404: Revert "bpo-23689: re module, fix memory leak when a match is terminated by a signal or allocation failure (GH-32283) (#93882)
Revert "bpo-23689: re module, fix memory leak when a match is terminated by a signal or memory allocation failure (GH-32283)" This reverts commit 6e3eee5c11b539e9aab39cff783acf57838c355a. Manual fixups to increase the MAGIC number and to handle conflicts with a couple of changes that landed after that. Thanks for reviews by Ma Lin and Serhiy Storchaka.
Diffstat (limited to 'Modules/_sre/sre_lib.h')
-rw-r--r--Modules/_sre/sre_lib.h30
1 files changed, 17 insertions, 13 deletions
diff --git a/Modules/_sre/sre_lib.h b/Modules/_sre/sre_lib.h
index 1e5b501..fb4c18b 100644
--- a/Modules/_sre/sre_lib.h
+++ b/Modules/_sre/sre_lib.h
@@ -1079,12 +1079,17 @@ dispatch:
by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */
/* <REPEAT> <skip> <1=min> <2=max>
<3=repeat_index> item <UNTIL> tail */
- TRACE(("|%p|%p|REPEAT %d %d %d\n", pattern, ptr,
- pattern[1], pattern[2], pattern[3]));
-
- /* install repeat context */
- ctx->u.rep = &state->repeats_array[pattern[3]];
+ TRACE(("|%p|%p|REPEAT %d %d\n", pattern, ptr,
+ pattern[1], pattern[2]));
+ /* install new repeat context */
+ /* TODO(https://github.com/python/cpython/issues/67877): Fix this
+ * potential memory leak. */
+ 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 = pattern;
ctx->u.rep->prev = state->repeat;
@@ -1094,6 +1099,7 @@ dispatch:
state->ptr = ptr;
DO_JUMP(JUMP_REPEAT, jump_repeat, pattern+pattern[0]);
state->repeat = ctx->u.rep->prev;
+ PyObject_Free(ctx->u.rep);
if (ret) {
RETURN_ON_ERROR(ret);
@@ -1103,8 +1109,7 @@ dispatch:
TARGET(SRE_OP_MAX_UNTIL):
/* maximizing repeat */
- /* <REPEAT> <skip> <1=min> <2=max>
- <3=repeat_index> item <MAX_UNTIL> tail */
+ /* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */
/* FIXME: we probably need to deal with zero-width
matches in here... */
@@ -1124,7 +1129,7 @@ dispatch:
/* not enough matches */
ctx->u.rep->count = ctx->count;
DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
- ctx->u.rep->pattern+4);
+ ctx->u.rep->pattern+3);
if (ret) {
RETURN_ON_ERROR(ret);
RETURN_SUCCESS;
@@ -1146,7 +1151,7 @@ dispatch:
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+4);
+ ctx->u.rep->pattern+3);
DATA_POP(&ctx->u.rep->last_ptr);
if (ret) {
MARK_POP_DISCARD(ctx->lastmark);
@@ -1171,8 +1176,7 @@ dispatch:
TARGET(SRE_OP_MIN_UNTIL):
/* minimizing repeat */
- /* <REPEAT> <skip> <1=min> <2=max>
- <3=repeat_index> item <MIN_UNTIL> tail */
+ /* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */
ctx->u.rep = state->repeat;
if (!ctx->u.rep)
@@ -1189,7 +1193,7 @@ dispatch:
/* not enough matches */
ctx->u.rep->count = ctx->count;
DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
- ctx->u.rep->pattern+4);
+ ctx->u.rep->pattern+3);
if (ret) {
RETURN_ON_ERROR(ret);
RETURN_SUCCESS;
@@ -1232,7 +1236,7 @@ dispatch:
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+4);
+ ctx->u.rep->pattern+3);
DATA_POP(&ctx->u.rep->last_ptr);
if (ret) {
RETURN_ON_ERROR(ret);