Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2e9c239

Browse files
committed
Extract environment setup and exception checking boilerplate logic
Changes: - Simplify exception handling in test harnesses via `handle_exception(e)` in the `except Exception as e:` block. - `setup_git_environment` is a step towards centralizing environment variable and logging configuration set up consistently across different fuzzing scripts. **Only applying it to a single test for now is an intentional choice in case it fails to work in the ClusterFuzz environment!** If it proves successful, a follow-up change set will be welcome.
1 parent799b9ca commit2e9c239

File tree

2 files changed

+95
-62
lines changed

2 files changed

+95
-62
lines changed

‎fuzzing/fuzz-targets/fuzz_submodule.py

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,17 @@
1-
# ruff: noqa: E402
21
importatheris
32
importsys
43
importos
5-
importtraceback
64
importtempfile
75
fromconfigparserimportParsingError
8-
fromutilsimportget_max_filename_length
9-
importre
10-
11-
bundle_dir=os.path.dirname(os.path.abspath(__file__))
12-
13-
ifgetattr(sys,"frozen",False)andhasattr(sys,"_MEIPASS"):# pragma: no cover
14-
bundled_git_binary_path=os.path.join(bundle_dir,"git")
15-
os.environ["GIT_PYTHON_GIT_EXECUTABLE"]=bundled_git_binary_path
16-
176
fromgitimportRepo,GitCommandError,InvalidGitRepositoryError
7+
fromutilsimport (
8+
setup_git_environment,
9+
handle_exception,
10+
get_max_filename_length,
11+
)
1812

19-
20-
defload_exception_list(file_path):
21-
"""Load and parse the exception list from a file."""
22-
try:
23-
withopen(file_path,"r")asfile:
24-
lines=file.readlines()
25-
exception_list=set()
26-
forlineinlines:
27-
match=re.match(r"(.+):(\d+):",line)
28-
ifmatch:
29-
file_path=match.group(1).strip()
30-
line_number=int(match.group(2).strip())
31-
exception_list.add((file_path,line_number))
32-
returnexception_list
33-
exceptFileNotFoundError:
34-
print(f"File not found:{file_path}")
35-
returnset()
36-
exceptExceptionase:
37-
print(f"Error loading exception list:{e}")
38-
returnset()
39-
40-
41-
defmatch_exception_with_traceback(exception_list,exc_traceback):
42-
"""Match exception traceback with the entries in the exception list."""
43-
forfilename,lineno,_,_intraceback.extract_tb(exc_traceback):
44-
forfile_pattern,line_patterninexception_list:
45-
ifre.fullmatch(file_pattern,filename)andre.fullmatch(line_pattern,str(lineno)):
46-
returnTrue
47-
returnFalse
48-
49-
50-
defcheck_exception_against_list(exception_list,exc_traceback):
51-
"""Check if the exception traceback matches any entry in the exception list."""
52-
returnmatch_exception_with_traceback(exception_list,exc_traceback)
53-
54-
55-
ifnotsys.warnoptions:# pragma: no cover
56-
# The warnings filter below can be overridden by passing the -W option
57-
# to the Python interpreter command line or setting the `PYTHONWARNINGS` environment variable.
58-
importwarnings
59-
importlogging
60-
61-
# Fuzzing data causes some modules to generate a large number of warnings
62-
# which are not usually interesting and make the test output hard to read, so we ignore them.
63-
warnings.simplefilter("ignore")
64-
logging.getLogger().setLevel(logging.ERROR)
13+
# Setup the git environment
14+
setup_git_environment()
6515

6616

6717
defTestOneInput(data):
@@ -131,12 +81,10 @@ def TestOneInput(data):
13181
):
13282
return-1
13383
exceptExceptionase:
134-
exc_traceback=e.__traceback__
135-
exception_list=load_exception_list(os.path.join(bundle_dir,"explicit-exceptions-list.txt"))
136-
ifcheck_exception_against_list(exception_list,exc_traceback):
84+
ifisinstance(e,ValueError)and"embedded null byte"instr(e):
13785
return-1
13886
else:
139-
raisee
87+
returnhandle_exception(e)
14088

14189

14290
defmain():

‎fuzzing/fuzz-targets/utils.py

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
importatheris# pragma: no cover
22
importos# pragma: no cover
3-
fromtypingimportList# pragma: no cover
3+
importre# pragma: no cover
4+
importtraceback# pragma: no cover
5+
importsys# pragma: no cover
6+
fromtypingimportSet,Tuple,List# pragma: no cover
47

58

69
@atheris.instrument_func
@@ -35,3 +38,85 @@ def get_max_filename_length(path: str) -> int: # pragma: no cover
3538
int: The maximum filename length.
3639
"""
3740
returnos.pathconf(path,"PC_NAME_MAX")
41+
42+
43+
@atheris.instrument_func
44+
defread_lines_from_file(file_path:str)->list:
45+
"""Read lines from a file and return them as a list."""
46+
try:
47+
withopen(file_path,"r")asf:
48+
return [line.strip()forlineinfifline.strip()]
49+
exceptFileNotFoundError:
50+
print(f"File not found:{file_path}")
51+
return []
52+
exceptIOErrorase:
53+
print(f"Error reading file{file_path}:{e}")
54+
return []
55+
56+
57+
@atheris.instrument_func
58+
defload_exception_list(file_path:str="explicit-exceptions-list.txt")->Set[Tuple[str,str]]:
59+
"""Load and parse the exception list from a default or specified file."""
60+
try:
61+
bundle_dir=os.path.dirname(os.path.abspath(__file__))
62+
full_path=os.path.join(bundle_dir,file_path)
63+
lines=read_lines_from_file(full_path)
64+
exception_list:Set[Tuple[str,str]]=set()
65+
forlineinlines:
66+
match=re.match(r"(.+):(\d+):",line)
67+
ifmatch:
68+
file_path:str=match.group(1).strip()
69+
line_number:str=str(match.group(2).strip())
70+
exception_list.add((file_path,line_number))
71+
returnexception_list
72+
exceptExceptionase:
73+
print(f"Error loading exception list:{e}")
74+
returnset()
75+
76+
77+
@atheris.instrument_func
78+
defmatch_exception_with_traceback(exception_list:Set[Tuple[str,str]],exc_traceback)->bool:
79+
"""Match exception traceback with the entries in the exception list."""
80+
forfilename,lineno,_,_intraceback.extract_tb(exc_traceback):
81+
forfile_pattern,line_patterninexception_list:
82+
# Ensure filename and line_number are strings for regex matching
83+
ifre.fullmatch(file_pattern,filename)andre.fullmatch(line_pattern,str(lineno)):
84+
returnTrue
85+
returnFalse
86+
87+
88+
@atheris.instrument_func
89+
defcheck_exception_against_list(exc_traceback,exception_file:str="explicit-exceptions-list.txt")->bool:
90+
"""Check if the exception traceback matches any entry in the exception list."""
91+
exception_list=load_exception_list(exception_file)
92+
returnmatch_exception_with_traceback(exception_list,exc_traceback)
93+
94+
95+
@atheris.instrument_func
96+
defhandle_exception(e:Exception)->int:
97+
"""Encapsulate exception handling logic for reusability."""
98+
exc_traceback=e.__traceback__
99+
ifcheck_exception_against_list(exc_traceback):
100+
return-1
101+
else:
102+
raisee
103+
104+
105+
@atheris.instrument_func
106+
defsetup_git_environment()->None:
107+
"""Set up the environment variables for Git."""
108+
bundle_dir=os.path.dirname(os.path.abspath(__file__))
109+
ifgetattr(sys,"frozen",False)andhasattr(sys,"_MEIPASS"):# pragma: no cover
110+
bundled_git_binary_path=os.path.join(bundle_dir,"git")
111+
os.environ["GIT_PYTHON_GIT_EXECUTABLE"]=bundled_git_binary_path
112+
113+
ifnotsys.warnoptions:# pragma: no cover
114+
# The warnings filter below can be overridden by passing the -W option
115+
# to the Python interpreter command line or setting the `PYTHONWARNINGS` environment variable.
116+
importwarnings
117+
importlogging
118+
119+
# Fuzzing data causes some modules to generate a large number of warnings
120+
# which are not usually interesting and make the test output hard to read, so we ignore them.
121+
warnings.simplefilter("ignore")
122+
logging.getLogger().setLevel(logging.ERROR)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp