Skip to content

Commit 6d586d5

Browse files
committed
Fix input file string handling
1 parent c4e48f8 commit 6d586d5

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

python/unicornafl/__init__.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ def __eq__(self, other):
9999
else:
100100
raise ValueError("Tried to compare UcAflError to {} ({})".format((type(other), other)))
101101

102+
def __handle_input_string(input_file: Optional[str | bytes]) -> int:
103+
if isinstance(input_file, str):
104+
return \
105+
ctypes.cast(
106+
ctypes.create_string_buffer(input_file.encode('utf-8')),
107+
ctypes.c_void_p
108+
).value
109+
elif isinstance(input_file, bytes):
110+
return \
111+
ctypes.cast(
112+
ctypes.create_string_buffer(input_file),
113+
ctypes.c_void_p
114+
).value
115+
elif input_file is None:
116+
return 0
117+
else:
118+
raise TypeError("Input file should be string or bytes or None")
102119

103120
def uc_afl_fuzz(uc: Uc,
104121
input_file: Optional[str | bytes],
@@ -129,22 +146,7 @@ def uc_afl_fuzz(uc: Uc,
129146
cb2 = ctypes.cast(UC_AFL_VALIDATE_CRASH_CB(
130147
_validate_crash_cb), UC_AFL_VALIDATE_CRASH_CB)
131148

132-
if isinstance(input_file, str):
133-
input_file = \
134-
ctypes.cast(
135-
ctypes.create_string_buffer(input_file.encode('utf-8')),
136-
ctypes.c_void_p
137-
).value
138-
elif isinstance(input_file, bytes):
139-
input_file = \
140-
ctypes.cast(
141-
ctypes.create_string_buffer(input_file),
142-
ctypes.c_void_p
143-
).value
144-
elif input_file is None:
145-
input_file = 0
146-
else:
147-
raise TypeError("Input file should be string or bytes or None")
149+
input_file = __handle_input_string(input_file)
148150
err = uc_afl_fuzz_impl(
149151
uc._uch.value,
150152
input_file,
@@ -192,14 +194,7 @@ def uc_afl_fuzz_custom(uc: Uc,
192194
cb3 = ctypes.cast(UC_AFL_FUZZ_CALLBACK_CB(
193195
_fuzz_callback_cb), UC_AFL_FUZZ_CALLBACK_CB)
194196

195-
if isinstance(input_file, str):
196-
input_file = ctypes.create_string_buffer(input_file.encode('utf-8'))
197-
elif isinstance(input_file, bytes):
198-
input_file = ctypes.create_string_buffer(input_file)
199-
elif input_file is None:
200-
input_file = 0
201-
else:
202-
raise TypeError("Input file should be string or bytes or None")
197+
input_file = __handle_input_string(input_file)
203198
err = uc_afl_fuzz_custom_impl(
204199
uc._uch.value,
205200
input_file,

0 commit comments

Comments
 (0)