Debian Codegraph @

testsuite/hashtable-overflow_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression test for the hashtable size*node_size integer overflow (KI-11/12).
3 #
4 # hashtable_create() once computed the slot-array byte count in 32-bit int
5 # (new_array0(char, size * node_size)); a large peer/data-driven size wrapped the
6 # product to a tiny value, under-allocating the table while tbl->size kept the
7 # huge size -- a later node access ran out of bounds (heap overflow / SEGV).
8 #
9 # The t_hashtable_overflow helper (built by make check) links the real
10 # hashtable.o, sets a realistic --max-alloc, and asks for an absurd size. The
11 # fix passes the factors separately so my_alloc's guard rejects it, exiting
12 # RERR_MALLOC; a regressed build under-allocates and crashes on the node access.
13
14 import subprocess
15
16 from rsyncfns import TOOLDIR, test_fail, test_skipped
17
18 RERR_MALLOC = 22 # errcode.h
19
20 helper = TOOLDIR / 't_hashtable_overflow'
21 if not helper.is_file():
22 test_skipped("t_hashtable_overflow helper not built")
23
24 proc = subprocess.run([str(helper)], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
25 timeout=30)
26 rc = proc.returncode
27 if rc == RERR_MALLOC:
28 print("hashtable-overflow: hashtable_create rejected the oversized size "
29 "(exited RERR_MALLOC) instead of under-allocating")
30 elif rc < 0:
31 test_fail(f"t_hashtable_overflow crashed (signal {-rc}): the hashtable "
32 "size*node_size integer overflow under-allocated the table\n"
33 + (proc.stderr or b'').decode('utf-8', 'replace'))
34 else:
35 test_fail(f"t_hashtable_overflow exited {rc}, expected RERR_MALLOC ({RERR_MALLOC}): "
36 "the oversized hashtable_create was not rejected\n"
37 + (proc.stderr or b'').decode('utf-8', 'replace'))