Skip to content

Commit 1e562d7

Browse files
committed
pythongh-109098: Fuzz re module instead of internal sre
1 parent 859618c commit 1e562d7

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

Modules/_xxtestfuzz/fuzzer.c

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -193,37 +193,33 @@ static int fuzz_json_loads(const char* data, size_t size) {
193193

194194
#define MAX_RE_TEST_SIZE 0x10000
195195

196-
PyObject* sre_compile_method = NULL;
197-
PyObject* sre_error_exception = NULL;
198-
int SRE_FLAG_DEBUG = 0;
196+
PyObject* re_compile_method = NULL;
197+
PyObject* re_error_exception = NULL;
198+
int RE_FLAG_DEBUG = 0;
199199
/* Called by LLVMFuzzerTestOneInput for initialization */
200200
static int init_sre_compile(void) {
201201
/* Import sre_compile.compile and sre.error */
202-
PyObject* sre_compile_module = PyImport_ImportModule("sre_compile");
203-
if (sre_compile_module == NULL) {
202+
PyObject* re_module = PyImport_ImportModule("re");
203+
if (re_module == NULL) {
204204
return 0;
205205
}
206-
sre_compile_method = PyObject_GetAttrString(sre_compile_module, "compile");
207-
if (sre_compile_method == NULL) {
206+
re_compile_method = PyObject_GetAttrString(re_module, "compile");
207+
if (re_compile_method == NULL) {
208208
return 0;
209209
}
210210

211-
PyObject* sre_constants = PyImport_ImportModule("sre_constants");
212-
if (sre_constants == NULL) {
211+
re_error_exception = PyObject_GetAttrString(re_module, "error");
212+
if (re_error_exception == NULL) {
213213
return 0;
214214
}
215-
sre_error_exception = PyObject_GetAttrString(sre_constants, "error");
216-
if (sre_error_exception == NULL) {
217-
return 0;
218-
}
219-
PyObject* debug_flag = PyObject_GetAttrString(sre_constants, "SRE_FLAG_DEBUG");
215+
PyObject* debug_flag = PyObject_GetAttrString(re_module, "DEBUG");
220216
if (debug_flag == NULL) {
221217
return 0;
222218
}
223-
SRE_FLAG_DEBUG = PyLong_AsLong(debug_flag);
219+
RE_FLAG_DEBUG = PyLong_AsLong(debug_flag);
224220
return 1;
225221
}
226-
/* Fuzz _sre.compile(x) */
222+
/* Fuzz re.compile(x) */
227223
static int fuzz_sre_compile(const char* data, size_t size) {
228224
/* Ignore really long regex patterns that will timeout the fuzzer */
229225
if (size > MAX_RE_TEST_SIZE) {
@@ -236,7 +232,7 @@ static int fuzz_sre_compile(const char* data, size_t size) {
236232
uint16_t flags = ((uint16_t*) data)[0];
237233
/* We remove the SRE_FLAG_DEBUG if present. This is because it
238234
prints to stdout which greatly decreases fuzzing speed */
239-
flags &= ~SRE_FLAG_DEBUG;
235+
flags &= ~RE_FLAG_DEBUG;
240236

241237
/* Pull the pattern from the remaining bytes */
242238
PyObject* pattern_bytes = PyBytes_FromStringAndSize(data + 2, size - 2);
@@ -249,9 +245,9 @@ static int fuzz_sre_compile(const char* data, size_t size) {
249245
return 0;
250246
}
251247

252-
/* compiled = _sre.compile(data[2:], data[0:2] */
248+
/* compiled = re.compile(data[2:], data[0:2] */
253249
PyObject* compiled = PyObject_CallFunctionObjArgs(
254-
sre_compile_method, pattern_bytes, flags_obj, NULL);
250+
re_compile_method, pattern_bytes, flags_obj, NULL);
255251
/* Ignore ValueError as the fuzzer will more than likely
256252
generate some invalid combination of flags */
257253
if (compiled == NULL && PyErr_ExceptionMatches(PyExc_ValueError)) {
@@ -267,7 +263,7 @@ static int fuzz_sre_compile(const char* data, size_t size) {
267263
PyErr_Clear();
268264
}
269265
/* Ignore re.error */
270-
if (compiled == NULL && PyErr_ExceptionMatches(sre_error_exception)) {
266+
if (compiled == NULL && PyErr_ExceptionMatches(re_error_exception)) {
271267
PyErr_Clear();
272268
}
273269

@@ -531,13 +527,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
531527
#if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_sre_compile)
532528
static int SRE_COMPILE_INITIALIZED = 0;
533529
if (!SRE_COMPILE_INITIALIZED && !init_sre_compile()) {
534-
if (!PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
535-
PyErr_Print();
536-
abort();
537-
}
538-
else {
539-
PyErr_Clear();
540-
}
530+
PyErr_Print();
531+
abort();
541532
} else {
542533
SRE_COMPILE_INITIALIZED = 1;
543534
}

0 commit comments

Comments
 (0)