Add graceful handling of expected exceptions in fuzz_submodule.py by DaveLak · Pull Request #1922 · gitpython-developers/GitPython
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"): if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"): # pragma: no cover path_to_bundled_git_binary = os.path.abspath(os.path.join(os.path.dirname(__file__), "git")) os.environ["GIT_PYTHON_GIT_EXECUTABLE"] = path_to_bundled_git_binary
with atheris.instrument_imports(): from git import Repo, GitCommandError, InvalidGitRepositoryError if not sys.warnoptions: # pragma: no cover # The warnings filter below can be overridden by passing the -W option # to the Python interpreter command line or setting the `PYTHONWARNINGS` environment variable. import warnings import logging
# Fuzzing data causes some modules to generate a large number of warnings # which are not usually interesting and make the test output hard to read, so we ignore them. warnings.simplefilter("ignore") logging.getLogger().setLevel(logging.ERROR)
def TestOneInput(data):
submodule.update(init=fdp.ConsumeBool(), dry_run=fdp.ConsumeBool(), force=fdp.ConsumeBool())
submodule_repo = submodule.module() new_file_path = os.path.join( submodule_repo.working_tree_dir, f"new_file_{fdp.ConsumeUnicodeNoSurrogates(fdp.ConsumeIntInRange(1, 512))}",
new_file_name = fdp.ConsumeUnicodeNoSurrogates( fdp.ConsumeIntInRange(1, max(1, get_max_filename_length(submodule_repo.working_tree_dir))) ) new_file_path = os.path.join(submodule_repo.working_tree_dir, new_file_name) with open(new_file_path, "wb") as new_file: new_file.write(fdp.ConsumeBytes(fdp.ConsumeIntInRange(1, 512))) submodule_repo.index.add([new_file_path])
except (ParsingError, GitCommandError, InvalidGitRepositoryError, FileNotFoundError, BrokenPipeError): except ( ParsingError, GitCommandError, InvalidGitRepositoryError, FileNotFoundError, FileExistsError, IsADirectoryError, NotADirectoryError, BrokenPipeError, ): return -1 except (ValueError, OSError) as e: except ValueError as e: expected_messages = [ "SHA is empty", "Reference at", "embedded null byte", "This submodule instance does not exist anymore", "cmd stdin was empty", "File name too long", ] if is_expected_exception_message(e, expected_messages): return -1
def main(): atheris.instrument_all() atheris.Setup(sys.argv, TestOneInput) atheris.Fuzz()