Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add fuzzer for dbm module
Fuzzes CPython's dbm backend — whichever implementation dbm.open()
selects on the build, typically the gdbm C module
(Modules/_gdbmmodule.c) or _dbm (Modules/_dbmmodule.c). Opens a fresh
GDBM-style database in a temp directory with mode "c", then drives a
sequence of up to 20 fuzzed operations — store (with fuzzed-length
keys and values), get, key listing via db.keys(), membership-guarded
delete, and iteration with value lookup — to exercise the hash-table
insert/lookup/delete paths and the on-disk file handling in the C
backend.
  • Loading branch information
AdamKorcz committed Apr 22, 2026
commit 9f96892a85898fb9b21ca1d795bd5a8f0237fa5f
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
all : fuzzer-html fuzzer-email fuzzer-httpclient fuzzer-json fuzzer-difflib fuzzer-csv fuzzer-decode fuzzer-ast fuzzer-tarfile fuzzer-tarfile-hypothesis fuzzer-zipfile fuzzer-zipfile-hypothesis fuzzer-re fuzzer-configparser fuzzer-tomllib fuzzer-plistlib fuzzer-xml fuzzer-zoneinfo fuzzer-binascii
all : fuzzer-html fuzzer-email fuzzer-httpclient fuzzer-json fuzzer-difflib fuzzer-csv fuzzer-decode fuzzer-ast fuzzer-tarfile fuzzer-tarfile-hypothesis fuzzer-zipfile fuzzer-zipfile-hypothesis fuzzer-re fuzzer-configparser fuzzer-tomllib fuzzer-plistlib fuzzer-xml fuzzer-zoneinfo fuzzer-binascii fuzzer-dbm

PYTHON_CONFIG_PATH=$(CPYTHON_INSTALL_PATH)/bin/python3-config
CXXFLAGS += $(shell $(PYTHON_CONFIG_PATH) --cflags)
Expand Down Expand Up @@ -43,3 +43,6 @@ fuzzer-zoneinfo:

fuzzer-binascii:
clang++ $(CXXFLAGS) $(LIB_FUZZING_ENGINE) -std=c++17 fuzzer.cpp -DPYTHON_HARNESS_PATH="\"binascii.py\"" -ldl $(LDFLAGS) -o fuzzer-binascii

fuzzer-dbm:
clang++ $(CXXFLAGS) $(LIB_FUZZING_ENGINE) -std=c++17 fuzzer.cpp -DPYTHON_HARNESS_PATH="\"dbm.py\"" -ldl $(LDFLAGS) -o fuzzer-dbm
56 changes: 56 additions & 0 deletions dbm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from fuzzeddataprovider import FuzzedDataProvider
import os
import dbm
import tempfile

OP_STORE = 0
OP_GET = 1
OP_LIST_KEYS = 2
OP_DELETE = 3
OP_ITERATE = 4


# Fuzzes the _gdbm C module (Modules/_gdbmmodule.c).
# Exercises key-value store operations on a temporary GDBM database:
# store, get, key listing, deletion, and iteration with fuzzed
# keys and values.
def FuzzerRunOne(FuzzerInput):
if len(FuzzerInput) < 1 or len(FuzzerInput) > 0x10000:
return
fdp = FuzzedDataProvider(FuzzerInput)
try:
with tempfile.TemporaryDirectory() as tmpdir:
dbpath = os.path.join(tmpdir, "fuzzdb")
with dbm.open(dbpath, "c") as db:
num_ops = fdp.ConsumeIntInRange(1, 20)
for _ in range(num_ops):
if fdp.remaining_bytes() == 0:
break
op = fdp.ConsumeIntInRange(OP_STORE, OP_ITERATE)
if op == OP_STORE:
n = fdp.ConsumeIntInRange(1, min(fdp.remaining_bytes(), 100))
key = fdp.ConsumeBytes(n)
n2 = (
fdp.ConsumeIntInRange(1, min(fdp.remaining_bytes(), 1000))
if fdp.remaining_bytes() > 0
else 0
)
val = fdp.ConsumeBytes(n2) if n2 > 0 else b""
db[key] = val
elif op == OP_GET:
n = fdp.ConsumeIntInRange(1, min(fdp.remaining_bytes(), 100))
key = fdp.ConsumeBytes(n)
_ = db.get(key)
elif op == OP_LIST_KEYS:
_ = list(db.keys())
elif op == OP_DELETE:
n = fdp.ConsumeIntInRange(1, min(fdp.remaining_bytes(), 100))
key = fdp.ConsumeBytes(n)
if key in db:
del db[key]
elif op == OP_ITERATE:
for k in db:
_ = db[k]
break
except Exception:
pass
1 change: 1 addition & 0 deletions fuzz_targets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ ast ast.py
binascii binascii.py
configparser configparser.py
csv csv.py
dbm dbm.py
decode decode.py
difflib difflib.py
email email.py
Expand Down
Loading