Skip to content

Commit 95fd4ed

Browse files
[3.12] gh-109098: Fuzz re module instead of internal sre (GH-109911) (#109932)
gh-109098: Fuzz re module instead of internal sre (GH-109911) * gh-109098: Fuzz re module instead of internal sre * Fix c-analyzer globals test failure * Put globals exception in ignored.tsv (cherry picked from commit a829356) Co-authored-by: Ammar Askar <[email protected]>
1 parent 1f62200 commit 95fd4ed

File tree

2 files changed

+21
-30
lines changed

2 files changed

+21
-30
lines changed

Modules/_xxtestfuzz/fuzzer.c

+18-27
Original file line numberDiff line numberDiff line change
@@ -188,37 +188,33 @@ static int fuzz_json_loads(const char* data, size_t size) {
188188

189189
#define MAX_RE_TEST_SIZE 0x10000
190190

191-
PyObject* sre_compile_method = NULL;
192-
PyObject* sre_error_exception = NULL;
193-
int SRE_FLAG_DEBUG = 0;
191+
PyObject* re_compile_method = NULL;
192+
PyObject* re_error_exception = NULL;
193+
int RE_FLAG_DEBUG = 0;
194194
/* Called by LLVMFuzzerTestOneInput for initialization */
195195
static int init_sre_compile(void) {
196196
/* Import sre_compile.compile and sre.error */
197-
PyObject* sre_compile_module = PyImport_ImportModule("sre_compile");
198-
if (sre_compile_module == NULL) {
197+
PyObject* re_module = PyImport_ImportModule("re");
198+
if (re_module == NULL) {
199199
return 0;
200200
}
201-
sre_compile_method = PyObject_GetAttrString(sre_compile_module, "compile");
202-
if (sre_compile_method == NULL) {
201+
re_compile_method = PyObject_GetAttrString(re_module, "compile");
202+
if (re_compile_method == NULL) {
203203
return 0;
204204
}
205205

206-
PyObject* sre_constants = PyImport_ImportModule("sre_constants");
207-
if (sre_constants == NULL) {
206+
re_error_exception = PyObject_GetAttrString(re_module, "error");
207+
if (re_error_exception == NULL) {
208208
return 0;
209209
}
210-
sre_error_exception = PyObject_GetAttrString(sre_constants, "error");
211-
if (sre_error_exception == NULL) {
212-
return 0;
213-
}
214-
PyObject* debug_flag = PyObject_GetAttrString(sre_constants, "SRE_FLAG_DEBUG");
210+
PyObject* debug_flag = PyObject_GetAttrString(re_module, "DEBUG");
215211
if (debug_flag == NULL) {
216212
return 0;
217213
}
218-
SRE_FLAG_DEBUG = PyLong_AsLong(debug_flag);
214+
RE_FLAG_DEBUG = PyLong_AsLong(debug_flag);
219215
return 1;
220216
}
221-
/* Fuzz _sre.compile(x) */
217+
/* Fuzz re.compile(x) */
222218
static int fuzz_sre_compile(const char* data, size_t size) {
223219
/* Ignore really long regex patterns that will timeout the fuzzer */
224220
if (size > MAX_RE_TEST_SIZE) {
@@ -231,7 +227,7 @@ static int fuzz_sre_compile(const char* data, size_t size) {
231227
uint16_t flags = ((uint16_t*) data)[0];
232228
/* We remove the SRE_FLAG_DEBUG if present. This is because it
233229
prints to stdout which greatly decreases fuzzing speed */
234-
flags &= ~SRE_FLAG_DEBUG;
230+
flags &= ~RE_FLAG_DEBUG;
235231

236232
/* Pull the pattern from the remaining bytes */
237233
PyObject* pattern_bytes = PyBytes_FromStringAndSize(data + 2, size - 2);
@@ -244,9 +240,9 @@ static int fuzz_sre_compile(const char* data, size_t size) {
244240
return 0;
245241
}
246242

247-
/* compiled = _sre.compile(data[2:], data[0:2] */
243+
/* compiled = re.compile(data[2:], data[0:2] */
248244
PyObject* compiled = PyObject_CallFunctionObjArgs(
249-
sre_compile_method, pattern_bytes, flags_obj, NULL);
245+
re_compile_method, pattern_bytes, flags_obj, NULL);
250246
/* Ignore ValueError as the fuzzer will more than likely
251247
generate some invalid combination of flags */
252248
if (compiled == NULL && PyErr_ExceptionMatches(PyExc_ValueError)) {
@@ -262,7 +258,7 @@ static int fuzz_sre_compile(const char* data, size_t size) {
262258
PyErr_Clear();
263259
}
264260
/* Ignore re.error */
265-
if (compiled == NULL && PyErr_ExceptionMatches(sre_error_exception)) {
261+
if (compiled == NULL && PyErr_ExceptionMatches(re_error_exception)) {
266262
PyErr_Clear();
267263
}
268264

@@ -526,13 +522,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
526522
#if !defined(_Py_FUZZ_ONE) || defined(_Py_FUZZ_fuzz_sre_compile)
527523
static int SRE_COMPILE_INITIALIZED = 0;
528524
if (!SRE_COMPILE_INITIALIZED && !init_sre_compile()) {
529-
if (!PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
530-
PyErr_Print();
531-
abort();
532-
}
533-
else {
534-
PyErr_Clear();
535-
}
525+
PyErr_Print();
526+
abort();
536527
} else {
537528
SRE_COMPILE_INITIALIZED = 1;
538529
}

Tools/c-analyzer/cpython/ignored.tsv

+3-3
Original file line numberDiff line numberDiff line change
@@ -572,15 +572,15 @@ Modules/_testmultiphase.c - uninitialized_def -
572572
Modules/_testsinglephase.c - global_state -
573573
Modules/_xxtestfuzz/_xxtestfuzz.c - _fuzzmodule -
574574
Modules/_xxtestfuzz/_xxtestfuzz.c - module_methods -
575-
Modules/_xxtestfuzz/fuzzer.c - SRE_FLAG_DEBUG -
575+
Modules/_xxtestfuzz/fuzzer.c - RE_FLAG_DEBUG -
576576
Modules/_xxtestfuzz/fuzzer.c - ast_literal_eval_method -
577577
Modules/_xxtestfuzz/fuzzer.c - compiled_patterns -
578578
Modules/_xxtestfuzz/fuzzer.c - csv_error -
579579
Modules/_xxtestfuzz/fuzzer.c - csv_module -
580580
Modules/_xxtestfuzz/fuzzer.c - json_loads_method -
581581
Modules/_xxtestfuzz/fuzzer.c - regex_patterns -
582-
Modules/_xxtestfuzz/fuzzer.c - sre_compile_method -
583-
Modules/_xxtestfuzz/fuzzer.c - sre_error_exception -
582+
Modules/_xxtestfuzz/fuzzer.c - re_compile_method -
583+
Modules/_xxtestfuzz/fuzzer.c - re_error_exception -
584584
Modules/_xxtestfuzz/fuzzer.c - struct_error -
585585
Modules/_xxtestfuzz/fuzzer.c - struct_unpack_method -
586586
Modules/_xxtestfuzz/fuzzer.c LLVMFuzzerTestOneInput CSV_READER_INITIALIZED -

0 commit comments

Comments
 (0)