Fix leak in Find Packet searching tree details

The string used to search the selected packet's protocol tree for
the field we already found is leaked.

The function prototype is prone to leaks; all the components of the
match_data are filled in inside the function, and it only needs to
return the field info. Restructure it so that the match_data is
created (and the string freed) inside the function, and only
the field_info is returned.
This commit is contained in:
John Thacker 2023-03-22 07:22:06 -04:00
parent 1c0a094e93
commit 2b45c16f1a
3 changed files with 15 additions and 17 deletions

18
file.c
View File

@ -3070,16 +3070,18 @@ cf_find_packet_protocol_tree(capture_file *cf, const char *string,
return find_packet(cf, match_protocol_tree, &mdata, dir);
}
gboolean
cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree, match_data *mdata)
field_info*
cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree)
{
mdata->frame_matched = FALSE;
mdata->string = convert_string_case(cf->sfilter, cf->case_type);
mdata->string_len = strlen(mdata->string);
mdata->cf = cf;
match_data mdata;
mdata.frame_matched = FALSE;
mdata.string = convert_string_case(cf->sfilter, cf->case_type);
mdata.string_len = strlen(mdata.string);
mdata.cf = cf;
/* Iterate through all the nodes looking for matching text */
proto_tree_children_foreach(tree, match_subtree_text, mdata);
return mdata->frame_matched ? MR_MATCHED : MR_NOTMATCHED;
proto_tree_children_foreach(tree, match_subtree_text, &mdata);
g_free((char *)mdata.string);
return mdata.frame_matched ? mdata.finfo : NULL;
}
static match_result

9
file.h
View File

@ -518,15 +518,14 @@ gboolean cf_find_packet_protocol_tree(capture_file *cf, const char *string,
search_direction dir);
/**
* Find field with a label that contains text string cfile->sfilter.
* Find field with a label that contains the text string cfile->sfilter in
* a protocol tree.
*
* @param cf the capture file
* @param tree the protocol tree
* @param mdata the first field (mdata->finfo) that matched the string
* @return TRUE if a packet was found, FALSE otherwise
* @return The first field in the tree that matched the string if found, NULL otherwise
*/
extern gboolean cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree,
match_data *mdata);
extern field_info* cf_find_string_protocol_tree(capture_file *cf, proto_tree *tree);
/**
* Find packet whose summary line contains a specified text string.

View File

@ -554,15 +554,12 @@ void PacketList::selectionChanged (const QItemSelection & selected, const QItemS
}
if (cap_file_->search_in_progress) {
match_data mdata;
field_info *fi = NULL;
if (cap_file_->string && cap_file_->decode_data) {
// The tree where the target string matched one of the labels was discarded in
// match_protocol_tree() so we have to search again in the latest tree.
if (cf_find_string_protocol_tree(cap_file_, cap_file_->edt->tree, &mdata)) {
fi = mdata.finfo;
}
fi = cf_find_string_protocol_tree(cap_file_, cap_file_->edt->tree);
} else if (cap_file_->search_pos != 0) {
// Find the finfo that corresponds to our byte.
fi = proto_find_field_from_offset(cap_file_->edt->tree, cap_file_->search_pos,