Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 33accc1

Browse files
author
Fredrik Lundh
committed
don't mistake memory errors (including reaching the recursion limit)
with success. also, check return values from the mark functions. this addresses (but doesn't really solve) bug #112693, and low-memory problems reported by jack jansen.
1 parent 6f9320b commit 33accc1

1 file changed

Lines changed: 24 additions & 18 deletions

File tree

Modules/_sre.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* 00-08-03 fl added recursion limit
1717
* 00-08-07 fl use PyOS_CheckStack() if available
1818
* 00-08-08 fl changed findall to return empty strings instead of None
19+
* 00-08-27 fl properly propagate memory errors
1920
*
2021
* Copyright (c) 1997-2000 by Secret Labs AB. All rights reserved.
2122
*
@@ -58,18 +59,17 @@ char copyright[] = " SRE 0.9.8 Copyright (c) 1997-2000 by Secret Labs AB ";
5859
/* -------------------------------------------------------------------- */
5960
/* optional features */
6061

61-
/* prevent run-away recursion (bad patterns on long strings)
62-
Require a smaller recursion limit for a number of 64-bit platforms
63-
to prevent stack overflow:
64-
Win64 - MS_WIN64, Linux64 - __LP64__, Monterey (64-bit AIX) - _LP64
65-
XXX Or maybe this should be defined for all SIZEOF_VOIDP>4 platforms?
66-
*/
62+
/* prevent run-away recursion (bad patterns on long strings) */
63+
6764
#if !defined(USE_STACKCHECK)
68-
# if defined(MS_WIN64) || defined(__LP64__) || defined(_LP64)
69-
# define USE_RECURSION_LIMIT 7500
70-
# else
71-
# define USE_RECURSION_LIMIT 10000
72-
# endif
65+
#if defined(MS_WIN64) || defined(__LP64__) || defined(_LP64)
66+
/* require smaller recursion limit for a number of 64-bit platforms:
67+
Win64 (MS_WIN64), Linux64 (__LP64__), Monterey (64-bit AIX) (_LP64) */
68+
/* FIXME: maybe the limit should be 40000 / sizeof(void*) ? */
69+
#define USE_RECURSION_LIMIT 7500
70+
#else
71+
#define USE_RECURSION_LIMIT 10000
72+
#endif
7373
#endif
7474

7575
/* enables fast searching */
@@ -534,6 +534,7 @@ SRE_COUNT(SRE_STATE* state, SRE_CODE* pattern, int maxcount, int level)
534534
return ptr - (SRE_CHAR*) state->ptr;
535535
}
536536

537+
#if 0 /* not used in this release */
537538
LOCAL(int)
538539
SRE_INFO(SRE_STATE* state, SRE_CODE* pattern)
539540
{
@@ -559,6 +560,7 @@ SRE_INFO(SRE_STATE* state, SRE_CODE* pattern)
559560
}
560561
return pattern[0];
561562
}
563+
#endif
562564

563565
LOCAL(int)
564566
SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
@@ -875,7 +877,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
875877
state->ptr = ptr;
876878
i = SRE_MATCH(state, pattern + pattern[0], level + 1);
877879
if (i)
878-
return 1;
880+
return i;
879881
ptr--;
880882
count--;
881883
}
@@ -887,7 +889,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
887889
state->ptr = ptr;
888890
i = SRE_MATCH(state, pattern + pattern[0], level + 1);
889891
if (i)
890-
return 1;
892+
return i;
891893
ptr--;
892894
count--;
893895
if (state->lastmark > lastmark) {
@@ -956,12 +958,16 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
956958
match another item, do so */
957959
rp->count = count;
958960
lastmark = state->lastmark;
959-
mark_save(state, 0, lastmark);
961+
i = mark_save(state, 0, lastmark);
962+
if (i < 0)
963+
return i;
960964
/* RECURSIVE */
961965
i = SRE_MATCH(state, rp->pattern + 3, level + 1);
962966
if (i)
963967
return i;
964-
mark_restore(state, 0, lastmark);
968+
i = mark_restore(state, 0, lastmark);
969+
if (i < 0)
970+
return i;
965971
rp->count = count - 1;
966972
state->ptr = ptr;
967973
}
@@ -1698,10 +1704,10 @@ pattern_findall(PatternObject* self, PyObject* args)
16981704
break;
16991705
}
17001706

1701-
status = PyList_Append(list, item);
1702-
Py_DECREF(item);
1703-
if (status < 0)
1707+
if (PyList_Append(list, item) < 0) {
1708+
Py_DECREF(item);
17041709
goto error;
1710+
}
17051711

17061712
if (state.ptr == state.start)
17071713
state.start = (void*) ((char*) state.ptr + state.charsize);

0 commit comments

Comments
 (0)