2222import os
2323import atheris
2424
25+
2526def setup_thrift_imports ():
2627 """Set up the Python path to include Thrift libraries and generated code."""
2728
@@ -48,19 +49,21 @@ def setup_thrift_imports():
4849 sys .path .append (gen_path )
4950 print (sys .path )
5051
52+
5153setup_thrift_imports ()
5254
5355from thrift .transport import TTransport
5456from thrift .TSerialization import serialize , deserialize
5557from fuzz .ttypes import FuzzTest
5658
59+
5760def create_parser_fuzzer (protocol_factory_class ):
5861 """
5962 Create a parser fuzzer function for a specific protocol.
60-
63+
6164 Args:
6265 protocol_factory_class: The Thrift protocol factory class to use
63-
66+
6467 Returns:
6568 A function that can be used with atheris.Setup()
6669 """
@@ -71,25 +74,26 @@ def TestOneInput(data):
7174 try :
7275 # Create a memory buffer with the fuzzed data
7376 buf = TTransport .TMemoryBuffer (data )
74- transport = TTransport .TBufferedTransportFactory ().getTransport (buf )
77+ TTransport .TBufferedTransportFactory ().getTransport (buf )
7578 factory = protocol_factory_class (string_length_limit = 1000 , container_length_limit = 1000 )
7679
7780 # Try to deserialize the fuzzed data into the test class
78- test_instance = deserialize (FuzzTest (), data , factory )
81+ deserialize (FuzzTest (), data , factory )
7982
80- except Exception as e :
83+ except Exception :
8184 # We expect various exceptions during fuzzing
8285 pass
8386
8487 return TestOneInput
8588
89+
8690def create_roundtrip_fuzzer (protocol_factory_class ):
8791 """
8892 Create a roundtrip fuzzer function for a specific protocol.
89-
93+
9094 Args:
9195 protocol_factory_class: The Thrift protocol factory class to use
92-
96+
9397 Returns:
9498 A function that can be used with atheris.Setup()
9599 """
@@ -100,7 +104,7 @@ def TestOneInput(data):
100104 try :
101105 # Create a memory buffer with the fuzzed data
102106 buf = TTransport .TMemoryBuffer (data )
103- transport = TTransport .TBufferedTransportFactory ().getTransport (buf )
107+ TTransport .TBufferedTransportFactory ().getTransport (buf )
104108 factory = protocol_factory_class (string_length_limit = 1000 , container_length_limit = 1000 )
105109
106110 # Try to deserialize the fuzzed data into the test class
@@ -112,18 +116,19 @@ def TestOneInput(data):
112116 # Verify the objects are equal after a second deserialization
113117 assert test_instance == deserialized
114118
115- except AssertionError as e :
116- raise e
117- except Exception as e :
119+ except AssertionError :
120+ raise
121+ except Exception :
118122 # We expect various exceptions during fuzzing
119123 pass
120124
121125 return TestOneInput
122126
127+
123128def _run_fuzzer (fuzzer_function ):
124129 """
125130 Set up and run the fuzzer for a specific protocol.
126-
131+
127132 Args:
128133 fuzzer_function: The fuzzer function to use
129134 """
@@ -136,7 +141,7 @@ def _run_fuzzer(fuzzer_function):
136141def run_roundtrip_fuzzer (protocol_factory_class ):
137142 """
138143 Set up and run the fuzzer for a specific protocol.
139-
144+
140145 Args:
141146 protocol_factory_class: The Thrift protocol factory class to use
142147 """
@@ -146,8 +151,8 @@ def run_roundtrip_fuzzer(protocol_factory_class):
146151def run_parser_fuzzer (protocol_factory_class ):
147152 """
148153 Set up and run the fuzzer for a specific protocol.
149-
154+
150155 Args:
151156 protocol_factory_class: The Thrift protocol factory class to use
152157 """
153- _run_fuzzer (create_parser_fuzzer (protocol_factory_class ))
158+ _run_fuzzer (create_parser_fuzzer (protocol_factory_class ))
0 commit comments