Detect and replace bad allocation patterns (more)

Extension of !1413, to improve regex, detect
additional opportunities to replace
`g_malloc` with `g_new`, and fix them.
This commit is contained in:
Moshe Kaplan 2020-12-22 15:21:37 -05:00
parent 180b5e5dc0
commit 6bce7b859a
5 changed files with 17 additions and 22 deletions

View File

@ -334,7 +334,7 @@ static void add_mca_key( const guint8 mca[ IPA_SIZE ], const gchar* text, guint8
fprintf_hex( f2, key, KNX_KEY_LENGTH );
}
mca_key = (struct knx_keyring_mca_keys*) wmem_alloc( wmem_epan_scope(), sizeof( struct knx_keyring_mca_keys ) );
mca_key = wmem_new(wmem_epan_scope(), struct knx_keyring_mca_keys);
if( mca_key )
{
@ -381,7 +381,7 @@ static void add_ga_key( guint16 ga, const gchar* text, guint8 password_hash[], g
fprintf_hex( f2, key, KNX_KEY_LENGTH );
}
ga_key = (struct knx_keyring_ga_keys*) wmem_alloc( wmem_epan_scope(), sizeof( struct knx_keyring_ga_keys ) );
ga_key = wmem_new(wmem_epan_scope(), struct knx_keyring_ga_keys);
if( ga_key )
{
@ -419,7 +419,7 @@ static void add_ga_sender( guint16 ga, const gchar* text, FILE* f2 )
fprintf( f2, "GA %u/%u/%u sender %u.%u.%u\n", (ga >> 11) & 0x1F, (ga >> 8) & 0x7, ga & 0xFF, (ia >> 12) & 0xF, (ia >> 8) & 0xF, ia & 0xFF );
}
ga_sender = (struct knx_keyring_ga_senders*) wmem_alloc( wmem_epan_scope(), sizeof( struct knx_keyring_ga_senders ) );
ga_sender = wmem_new(wmem_epan_scope(), struct knx_keyring_ga_senders);
if( ga_sender )
{
@ -465,7 +465,7 @@ static void add_ia_key( guint16 ia, const gchar* text, guint8 password_hash[], g
fprintf_hex( f2, key, KNX_KEY_LENGTH );
}
ia_key = (struct knx_keyring_ia_keys*) wmem_alloc( wmem_epan_scope(), sizeof( struct knx_keyring_ia_keys ) );
ia_key = wmem_new(wmem_epan_scope(), struct knx_keyring_ia_keys);
if( ia_key )
{
@ -504,7 +504,7 @@ static void add_ia_seq( guint16 ia, const gchar* text, FILE* f2 )
fprintf( f2, "IA %u.%u.%u SeqNr %" G_GINT64_MODIFIER "u\n", (ia >> 12) & 0xF, (ia >> 8) & 0xF, ia & 0xFF, seq );
}
ia_seq = (struct knx_keyring_ia_seqs*) wmem_alloc( wmem_epan_scope(), sizeof( struct knx_keyring_ia_seqs ) );
ia_seq = wmem_new(wmem_epan_scope(), struct knx_keyring_ia_seqs);
if( ia_seq )
{

View File

@ -792,7 +792,7 @@ mkipv4_address( address **addr, const char *str_addr )
int ret;
char *addr_data;
*addr=(address *)g_malloc( sizeof(address) );
*addr=g_new(address, 1);
addr_data=(char *)g_malloc( 4 );
ret = str_to_ip(str_addr, addr_data);
if (ret)
@ -804,7 +804,7 @@ static void
parse_tuple( char *key_from_option )
{
char *client,*key;
tacplus_key_entry *tacplus_data=(tacplus_key_entry *)g_malloc( sizeof(tacplus_key_entry) );
tacplus_key_entry *tacplus_data=g_new(tacplus_key_entry, 1);
/*
ws_debug_printf("keys: %s\n", key_from_option );
*/

View File

@ -270,7 +270,7 @@ stats_tree_register_with_group(const char *tapname, const char *abbr, const char
stat_tree_packet_cb packet, stat_tree_init_cb init,
stat_tree_cleanup_cb cleanup, register_stat_group_t stat_group)
{
stats_tree_cfg *cfg = (stats_tree_cfg *)g_malloc0( sizeof(stats_tree_cfg) );
stats_tree_cfg *cfg = g_new0(stats_tree_cfg, 1);
/* at the very least the abbrev and the packet function should be given */
g_assert( tapname && abbr && packet );

View File

@ -19,29 +19,20 @@ print_replacement_info = True
patterns = [
# Replace (myobj *)g_malloc(sizeof(myobj)) with g_new(myobj, 1)
(re.compile(r'\(([^\s\*]+)\s*\*\)\s*g_malloc(0?)\s*\(sizeof\s*\(\1\)\)'), r'g_new\2(\1, 1)'),
# Replace (struct myobj *)g_malloc(sizeof(struct myobj)) with g_new(struct myobj, 1)
(re.compile(r'\((struct\s*[^\s\*]+)\s*\*\)\s*g_malloc(0?)\s*\(sizeof\s*\(\1\)\)'), r'g_new\2(\1, 1)'),
(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)'),
# Replace (myobj *)g_malloc(sizeof(myobj) * foo) with g_new(myobj, foo)
(re.compile(r'\(([^\s\*]+)\s*\*\)\s*g_malloc(0?)\s*\(sizeof\s*\(\1\)\s*\*\s*([^\s]+)\)'), r'g_new\2(\1, \3)'),
# Replace (struct myobj *)g_malloc(sizeof(struct myobj) * foo) with g_new(struct myobj, foo)
(re.compile(r'\((struct\s*[^\s\*]+)\s*\*\)\s*g_malloc(0?)\s*\(sizeof\s*\(\1\)\s*\*\s*([^\s]+)\)'), r'g_new\2(\1, \3)'),
(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)'),
# Replace (myobj *)g_malloc(foo * sizeof(myobj)) with g_new(myobj, foo)
(re.compile(r'\(([^\s\*]+)\s*\*\)\s*g_malloc(0?)\s*\(([^\s]+)\s*\*\s*sizeof\s*\(\1\)\)'), r'g_new\2(\1, \3)'),
# Replace (struct myobj *)g_malloc(foo * sizeof(struct myobj)) with g_new(struct myobj, foo)
(re.compile(r'\((struct\s*[^\s\*]+)\s*\*\)\s*g_malloc(0?)\s*\(([^\s]+)\s*\*\s*sizeof\s*\(\1\)\)'), r'g_new\2(\1, \3)'),
(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)'),
# Replace (myobj *)wmem_alloc(wmem_file_scope(), sizeof(myobj)) with wmem_new(wmem_file_scope(), myobj)
(re.compile(r'\(([^\s\*]+)\s*\*\)\s*wmem_alloc(0?)\s*\(\s*([_a-z\(\)->]+),\s*sizeof\s*\(\1\)\)'), r'wmem_new\2(\3, \1)'),
# Replace (struct myobj *)wmem_alloc(wmem_file_scope(), sizeof(struct myobj)) with wmem_new(wmem_file_scope(), struct myobj)
(re.compile(r'\((struct\s+[^\s\*]+)\s*\*\)\s*wmem_alloc(0?)\s*\(\s*([_a-z\(\)->]+),\s*sizeof\s*\(\1\)\)'), r'wmem_new\2(\3, \1)'),
(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)'),
]
def replace_file(fpath):
@ -81,6 +72,7 @@ def test_replacements():
(guint8 *)g_malloc(16 * sizeof(guint8))
(guint32 *)g_malloc(sizeof(guint32)*2)
(struct imf_field *)g_malloc (sizeof (struct imf_field))
(rtspstat_t *)g_malloc( sizeof(rtspstat_t) )
(proto_data_t *)wmem_alloc(scope, sizeof(proto_data_t))
(giop_sub_handle_t *)wmem_alloc(wmem_epan_scope(), sizeof (giop_sub_handle_t))
(mtp3_addr_pc_t *)wmem_alloc0(pinfo->pool, sizeof(mtp3_addr_pc_t))
@ -88,6 +80,7 @@ def test_replacements():
(dcerpc_matched_key *)wmem_alloc(wmem_file_scope(), sizeof (dcerpc_matched_key));
(struct smtp_session_state *)wmem_alloc0(wmem_file_scope(), sizeof(struct smtp_session_state))
(struct batman_packet_v5 *)wmem_alloc(wmem_packet_scope(), sizeof(struct batman_packet_v5))
(struct knx_keyring_mca_keys*) wmem_alloc( wmem_epan_scope(), sizeof( struct knx_keyring_mca_keys ) )
"""
expected_output = """\
g_new0(if_info_t, 1)
@ -95,6 +88,7 @@ g_new(oui_info_t, 1)
g_new(guint8, 16)
g_new(guint32, 2)
g_new(struct imf_field, 1)
g_new(rtspstat_t, 1)
wmem_new(scope, proto_data_t)
wmem_new(wmem_epan_scope(), giop_sub_handle_t)
wmem_new0(pinfo->pool, mtp3_addr_pc_t)
@ -102,6 +96,7 @@ wmem_new(wmem_file_scope(), dcerpc_bind_value)
wmem_new(wmem_file_scope(), dcerpc_matched_key);
wmem_new0(wmem_file_scope(), struct smtp_session_state)
wmem_new(wmem_packet_scope(), struct batman_packet_v5)
wmem_new(wmem_epan_scope(), struct knx_keyring_mca_keys)
"""
output = test_string
for pattern, replacewith in patterns:

View File

@ -231,7 +231,7 @@ rtspstat_init(const char *opt_arg, void *userdata _U_)
filter = NULL;
}
sp = (rtspstat_t *)g_malloc( sizeof(rtspstat_t) );
sp = g_new(rtspstat_t, 1);
sp->filter = g_strdup(filter);
/*g_hash_table_foreach( rtsp_status, (GHFunc)rtsp_reset_hash_responses, NULL);*/