Skip to content

Commit 3f15be8

Browse files
joydeep049terriko
andauthored
feat: tempfile cleanup for RParser (#3944)
* feat: added tempfile cleanup to RParser Co-authored-by: Joydeep Tripathy <[email protected]> Co-authored-by: Terri Oda <[email protected]>
1 parent 79a2624 commit 3f15be8

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

fuzz/fuzz_renv_lock.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
This module contains fuzz testing for the RParser's handling of renv.lock files.
66
"""
77

8+
import os
9+
import shutil
810
import sys
911
import tempfile
10-
from pathlib import Path
1112

1213
import atheris
1314
import atheris_libprotobuf_mutator
@@ -25,12 +26,13 @@
2526
logger = LOGGER.getChild("Fuzz")
2627

2728

28-
def RenvLockBuilder(data):
29+
def RenvLockBuilder(data, file_path):
2930
"""
3031
This function converts the given data into a renv.lock file.
3132
3233
Args:
3334
data (protobuf message): The protobuf message to convert and process.
35+
file_path: The path of the file to build.
3436
"""
3537
# Parse the JSON data
3638
json_data = MessageToDict(
@@ -89,13 +91,16 @@ def RenvLockBuilder(data):
8991
f.write("}\n")
9092

9193

92-
def TestParseData(data):
94+
def TestParseData(data, cve_db, logger, tmpdir):
9395
"""
94-
Fuzz testing function for the RParser's handling of renv.lock files.
95-
96+
Fuzz test the RustParser's handling of renv.lock files.
9697
Args:
97-
data (protobuf message): The protobuf message to convert and process.
98+
data (protobuf message): The protobuf message to convert to a renv.lock file.
99+
cve_db: Object for the Database of CVE-BIN-TOOL.
100+
logger: Logger object.
101+
tmpdir: The temporary direct object.
98102
"""
103+
file_path = os.path.join(tmpdir, "renv.lock")
99104
try:
100105
RenvLockBuilder(data)
101106

@@ -106,7 +111,20 @@ def TestParseData(data):
106111
return
107112

108113

109-
file_path = str(Path(tempfile.mkdtemp(prefix="cve-bin-tool-")) / "renv.lock")
110-
111-
atheris_libprotobuf_mutator.Setup(sys.argv, TestParseData, proto=renv_lock_pb2.RenvLock)
112-
atheris.Fuzz()
114+
def main():
115+
"""Main Function to Run Fuzzing and Facilitate Tempfile cleanup."""
116+
tmpdir = tempfile.mkdtemp(prefix="cve-bin-tool-fuzz-renv-")
117+
try:
118+
atheris_libprotobuf_mutator.Setup(
119+
sys.argv,
120+
lambda data: TestParseData(data, cve_db, logger, tmpdir),
121+
proto=renv_lock_pb2.RenvLock,
122+
)
123+
atheris.Fuzz()
124+
finally:
125+
if os.path.exists(tmpdir):
126+
shutil.rmtree(tmpdir)
127+
128+
129+
if __name__ == "__main__":
130+
main()

0 commit comments

Comments
 (0)