2 Detect and replace instances of g_malloc() and wmem_alloc() with
3 g_new() wmem_new(), to improve the readability of Wireshark's code.
5 Also detect and replace instances of
6 g_malloc(sizeof(struct myobj) * foo)
8 g_new(struct myobj, foo)
9 to better prevent integer overflows
11 SPDX-License-Identifier: MIT
18 print_replacement_info
= True
21 # Replace (myobj *)g_malloc(sizeof(myobj)) with g_new(myobj, 1)
22 # Replace (struct myobj *)g_malloc(sizeof(struct myobj)) with g_new(struct myobj, 1)
23 (re
.compile(r
'\(\s*([struct]{0,6}\s*[^\s\*]+)\s*\*\s*\)\s*g_malloc(0?)\s*\(\s*sizeof\s*\(\s*\1\s*\)\s*\)'), r
'g_new\2(\1, 1)'),
25 # Replace (myobj *)g_malloc(sizeof(myobj) * foo) with g_new(myobj, foo)
26 # Replace (struct myobj *)g_malloc(sizeof(struct myobj) * foo) with g_new(struct myobj, foo)
27 (re
.compile(r
'\(\s*([struct]{0,6}\s*[^\s\*]+)\s*\*\s*\)\s*g_malloc(0?)\s*\(\s*sizeof\s*\(\s*\1\s*\)\s*\*\s*([^\s]+)\s*\)'), r
'g_new\2(\1, \3)'),
29 # Replace (myobj *)g_malloc(foo * sizeof(myobj)) with g_new(myobj, foo)
30 # Replace (struct myobj *)g_malloc(foo * sizeof(struct myobj)) with g_new(struct myobj, foo)
31 (re
.compile(r
'\(\s*([struct]{0,6}\s*[^\s\*]+)\s*\*\s*\)\s*g_malloc(0?)\s*\(\s*([^\s]+)\s*\*\s*sizeof\s*\(\s*\1\s*\)\s*\)'), r
'g_new\2(\1, \3)'),
33 # Replace (myobj *)wmem_alloc(wmem_file_scope(), sizeof(myobj)) with wmem_new(wmem_file_scope(), myobj)
34 # Replace (struct myobj *)wmem_alloc(wmem_file_scope(), sizeof(struct myobj)) with wmem_new(wmem_file_scope(), struct myobj)
35 (re
.compile(r
'\(\s*([struct]{0,6}\s*[^\s\*]+)\s*\*\s*\)\s*wmem_alloc(0?)\s*\(\s*([_a-z\(\)->]+),\s*sizeof\s*\(\s*\1\s*\)\s*\)'), r
'wmem_new\2(\3, \1)'),
38 def replace_file(fpath
):
39 with
open(fpath
, 'r') as fh
:
40 fdata_orig
= fh
.read()
42 for pattern
, replacewith
in patterns
:
43 fdata_out
= pattern
.sub(replacewith
, fdata
)
44 if print_replacement_info
and fdata
!= fdata_out
:
45 for match
in re
.finditer(pattern
, fdata
):
46 replacement
= re
.sub(pattern
, replacewith
, match
.group(0))
47 print("Bad malloc pattern in %s: Replace '%s' with '%s'" % (fpath
, match
.group(0), replacement
))
49 if fdata_out
!= fdata_orig
:
50 with
open(fpath
, 'w') as fh
:
54 def run_specific_files(fpaths
):
56 if not (fpath
.endswith('.c') or fpath
.endswith('.cpp')):
60 def run_recursive(root_dir
):
61 for root
, dirs
, files
in os
.walk(root_dir
):
64 fpath
= os
.path
.join(root
, fname
)
66 run_specific_files(fpaths
)
68 def test_replacements():
70 (if_info_t*) g_malloc0(sizeof(if_info_t))
71 (oui_info_t *)g_malloc(sizeof (oui_info_t))
72 (uint8_t *)g_malloc(16 * sizeof(uint8_t))
73 (uint32_t *)g_malloc(sizeof(uint32_t)*2)
74 (struct imf_field *)g_malloc (sizeof (struct imf_field))
75 (rtspstat_t *)g_malloc( sizeof(rtspstat_t) )
76 (proto_data_t *)wmem_alloc(scope, sizeof(proto_data_t))
77 (giop_sub_handle_t *)wmem_alloc(wmem_epan_scope(), sizeof (giop_sub_handle_t))
78 (mtp3_addr_pc_t *)wmem_alloc0(pinfo->pool, sizeof(mtp3_addr_pc_t))
79 (dcerpc_bind_value *)wmem_alloc(wmem_file_scope(), sizeof (dcerpc_bind_value))
80 (dcerpc_matched_key *)wmem_alloc(wmem_file_scope(), sizeof (dcerpc_matched_key));
81 (struct smtp_session_state *)wmem_alloc0(wmem_file_scope(), sizeof(struct smtp_session_state))
82 (struct batman_packet_v5 *)wmem_alloc(pinfo->pool, sizeof(struct batman_packet_v5))
83 (struct knx_keyring_mca_keys*) wmem_alloc( wmem_epan_scope(), sizeof( struct knx_keyring_mca_keys ) )
85 expected_output
= """\
90 g_new(struct imf_field, 1)
92 wmem_new(scope, proto_data_t)
93 wmem_new(wmem_epan_scope(), giop_sub_handle_t)
94 wmem_new0(pinfo->pool, mtp3_addr_pc_t)
95 wmem_new(wmem_file_scope(), dcerpc_bind_value)
96 wmem_new(wmem_file_scope(), dcerpc_matched_key);
97 wmem_new0(wmem_file_scope(), struct smtp_session_state)
98 wmem_new(pinfo->pool, struct batman_packet_v5)
99 wmem_new(wmem_epan_scope(), struct knx_keyring_mca_keys)
102 for pattern
, replacewith
in patterns
:
103 output
= pattern
.sub(replacewith
, output
)
104 assert(output
== expected_output
)
108 if len(sys
.argv
) == 2:
109 root_dir
= sys
.argv
[1]
110 run_recursive(root_dir
)
113 for line
in sys
.stdin
:
117 run_specific_files(fpaths
)
119 if __name__
== "__main__":