|
| 1 | +import atheris |
| 2 | +import io |
| 3 | +import sys |
| 4 | +import os |
| 5 | +import tempfile |
| 6 | + |
| 7 | +if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"): |
| 8 | + path_to_bundled_git_binary = os.path.abspath(os.path.join(os.path.dirname(__file__), "git")) |
| 9 | + os.environ["GIT_PYTHON_GIT_EXECUTABLE"] = path_to_bundled_git_binary |
| 10 | + |
| 11 | +with atheris.instrument_imports(): |
| 12 | + import git |
| 13 | + |
| 14 | + |
| 15 | +def TestOneInput(data): |
| 16 | + fdp = atheris.FuzzedDataProvider(data) |
| 17 | + |
| 18 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 19 | + repo = git.Repo.init(path=temp_dir) |
| 20 | + |
| 21 | + # Generate a minimal set of files based on fuzz data to minimize I/O operations. |
| 22 | + file_paths = [os.path.join(temp_dir, f"File{i}") for i in range(min(3, fdp.ConsumeIntInRange(1, 3)))] |
| 23 | + for file_path in file_paths: |
| 24 | + with open(file_path, "wb") as f: |
| 25 | + # The chosen upperbound for count of bytes we consume by writing to these |
| 26 | + # files is somewhat arbitrary and may be worth experimenting with if the |
| 27 | + # fuzzer coverage plateaus. |
| 28 | + f.write(fdp.ConsumeBytes(fdp.ConsumeIntInRange(1, 512))) |
| 29 | + |
| 30 | + repo.index.add(file_paths) |
| 31 | + repo.index.commit(fdp.ConsumeUnicodeNoSurrogates(fdp.ConsumeIntInRange(1, 80))) |
| 32 | + |
| 33 | + fuzz_tree = git.Tree(repo, git.Tree.NULL_BIN_SHA, 0, "") |
| 34 | + |
| 35 | + try: |
| 36 | + fuzz_tree._deserialize(io.BytesIO(data)) |
| 37 | + except IndexError: |
| 38 | + return -1 |
| 39 | + |
| 40 | + |
| 41 | +def main(): |
| 42 | + atheris.Setup(sys.argv, TestOneInput) |
| 43 | + atheris.Fuzz() |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + main() |
0 commit comments