Skip to content

Commit 8ed9bda

Browse files
[3.11] gh-109098: Fuzz re module instead of internal sre (GH-109911) (GH-109933)
* 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 c485715 commit 8ed9bda

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

Modules/_xxtestfuzz/fuzzer.c

+16-20
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) {
208-
return 0;
209-
}
210-
sre_error_exception = PyObject_GetAttrString(sre_constants, "error");
211-
if (sre_error_exception == NULL) {
206+
re_error_exception = PyObject_GetAttrString(re_module, "error");
207+
if (re_error_exception == NULL) {
212208
return 0;
213209
}
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

Tools/c-analyzer/cpython/ignored.tsv

+3-3
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,15 @@ Modules/_testmultiphase.c - testexport_methods -
243243
Modules/_testmultiphase.c - uninitialized_def -
244244
Modules/_xxtestfuzz/_xxtestfuzz.c - _fuzzmodule -
245245
Modules/_xxtestfuzz/_xxtestfuzz.c - module_methods -
246-
Modules/_xxtestfuzz/fuzzer.c - SRE_FLAG_DEBUG -
246+
Modules/_xxtestfuzz/fuzzer.c - RE_FLAG_DEBUG -
247247
Modules/_xxtestfuzz/fuzzer.c - ast_literal_eval_method -
248248
Modules/_xxtestfuzz/fuzzer.c - compiled_patterns -
249249
Modules/_xxtestfuzz/fuzzer.c - csv_error -
250250
Modules/_xxtestfuzz/fuzzer.c - csv_module -
251251
Modules/_xxtestfuzz/fuzzer.c - json_loads_method -
252252
Modules/_xxtestfuzz/fuzzer.c - regex_patterns -
253-
Modules/_xxtestfuzz/fuzzer.c - sre_compile_method -
254-
Modules/_xxtestfuzz/fuzzer.c - sre_error_exception -
253+
Modules/_xxtestfuzz/fuzzer.c - re_compile_method -
254+
Modules/_xxtestfuzz/fuzzer.c - re_error_exception -
255255
Modules/_xxtestfuzz/fuzzer.c - struct_error -
256256
Modules/_xxtestfuzz/fuzzer.c - struct_unpack_method -
257257
Modules/_xxtestfuzz/fuzzer.c LLVMFuzzerTestOneInput CSV_READER_INITIALIZED -

0 commit comments

Comments
 (0)