From f9ac0f40f715666439fb44b77c7620ba6427d490 Mon Sep 17 00:00:00 2001 From: Evan Huus Date: Mon, 4 Oct 2021 13:35:37 -0400 Subject: [PATCH] to_str: scope decode_bits_in_field Mostly straightforward. The only complication was proto_tree_add_split_bits_crumb which needed some manipulation to guarantee a non-null tree so we could use its memory scope. This is one of the last non-dissector uses of wmem_packet_scope! --- epan/dissectors/packet-gsm_a_dtap.c | 2 +- epan/dissectors/packet-gsm_a_gm.c | 4 ++-- epan/dissectors/packet-gsm_a_rr.c | 2 +- epan/dissectors/packet-per.c | 4 ++-- epan/proto.c | 21 +++++++++++++++------ epan/to_str.c | 4 ++-- epan/to_str.h | 2 +- 7 files changed, 24 insertions(+), 15 deletions(-) diff --git a/epan/dissectors/packet-gsm_a_dtap.c b/epan/dissectors/packet-gsm_a_dtap.c index e230e3ba04..6dfe8175d8 100644 --- a/epan/dissectors/packet-gsm_a_dtap.c +++ b/epan/dissectors/packet-gsm_a_dtap.c @@ -3959,7 +3959,7 @@ de_tp_epc_ellipsoid_point_with_alt(tvbuff_t *tvb, proto_tree *tree, packet_info curr_offset += 3; longitude = tvb_get_ntoh24(tvb, curr_offset); proto_tree_add_int_format(tree, hf_gsm_a_dtap_epc_degrees_longitude, tvb, curr_offset, 3, longitude, - "%s = %s: %d", decode_bits_in_field(curr_offset<<3, 24, longitude, ENC_BIG_ENDIAN), + "%s = %s: %d", decode_bits_in_field(pinfo->pool, curr_offset<<3, 24, longitude, ENC_BIG_ENDIAN), proto_registrar_get_name(hf_gsm_a_dtap_epc_degrees_longitude), longitude-8388608); curr_offset += 3; proto_tree_add_bits_item(tree, hf_gsm_a_dtap_epc_altitude_dir, tvb, curr_offset<<3, 1, ENC_BIG_ENDIAN); diff --git a/epan/dissectors/packet-gsm_a_gm.c b/epan/dissectors/packet-gsm_a_gm.c index 8b4c8f0cdf..5c023b1ad3 100644 --- a/epan/dissectors/packet-gsm_a_gm.c +++ b/epan/dissectors/packet-gsm_a_gm.c @@ -1880,7 +1880,7 @@ de_gmm_ms_radio_acc_cap(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gui /* decode_bits_in_field(gint bit_offset, gint no_of_bits, guint64 value)*/ proto_tree_add_uint_format(tf_tree, hf_gsm_a_gm_rf_power_capability, tvb, curr_offset-1-add_octets, 1+add_octets, value, - "%s RF Power Capability, GMSK Power Class: %s (%u)", decode_bits_in_field(bit_offset, 3, value, ENC_BIG_ENDIAN), str, value); + "%s RF Power Capability, GMSK Power Class: %s (%u)", decode_bits_in_field(pinfo->pool, bit_offset, 3, value, ENC_BIG_ENDIAN), str, value); bit_offset += 3; curr_bits_length -= bits_needed; oct <<= bits_needed; @@ -1948,7 +1948,7 @@ de_gmm_ms_radio_acc_cap(tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo, gui str = "Not specified??"; proto_tree_add_uint_format(tf_tree, hf_gsm_a_gm_rf_power_capability, tvb, curr_offset-1-add_octets, 1+add_octets, value, - "%s RF Power Capability, GMSK Power Class: %s (%u)", decode_bits_in_field(bit_offset, 3, value, ENC_BIG_ENDIAN), str, value); + "%s RF Power Capability, GMSK Power Class: %s (%u)", decode_bits_in_field(pinfo->pool, bit_offset, 3, value, ENC_BIG_ENDIAN), str, value); bit_offset += 3; curr_bits_length -= bits_needed; diff --git a/epan/dissectors/packet-gsm_a_rr.c b/epan/dissectors/packet-gsm_a_rr.c index 745bde8fe8..1e471eb6f9 100644 --- a/epan/dissectors/packet-gsm_a_rr.c +++ b/epan/dissectors/packet-gsm_a_rr.c @@ -1599,7 +1599,7 @@ static void dissect_channel_list_n_range(tvbuff_t *tvb, proto_tree *tree, packet for (i=1; i<=imax; i++) { w[i] = (gint) tvb_get_bits(tvb, bit_offset, wsize, FALSE); proto_tree_add_bytes_format(subtree, hf_gsm_a_rr_w_elements, tvb, bit_offset>>3, ((bit_offset+wsize-1)>>3) - (bit_offset>>3) + 1 , NULL, "%s W(%d): %d", - decode_bits_in_field(bit_offset, wsize, w[i], ENC_BIG_ENDIAN), + decode_bits_in_field(pinfo->pool, bit_offset, wsize, w[i], ENC_BIG_ENDIAN), i, w[i]); bit_offset += wsize; diff --git a/epan/dissectors/packet-per.c b/epan/dissectors/packet-per.c index 3a5e2cdbbd..15f05965d7 100644 --- a/epan/dissectors/packet-per.c +++ b/epan/dissectors/packet-per.c @@ -1334,7 +1334,7 @@ DEBUG_ENTRY("dissect_per_constrained_integer"); val = (guint32)tvb_get_bits64(tvb,offset,num_bits,ENC_BIG_ENDIAN); if (display_internal_per_fields){ - str = decode_bits_in_field((offset&0x07),num_bits,val,ENC_BIG_ENDIAN); + str = decode_bits_in_field(actx->pinfo->pool, (offset&0x07),num_bits,val,ENC_BIG_ENDIAN); proto_tree_add_uint(tree, hf_per_internal_min, tvb, val_start,val_length, min); proto_tree_add_uint64(tree, hf_per_internal_range, tvb, val_start, val_length, range); proto_tree_add_uint(tree, hf_per_internal_num_bits, tvb, val_start, val_length, num_bits); @@ -2134,7 +2134,7 @@ static tvbuff_t *dissect_per_bit_string_display(tvbuff_t *tvb, guint32 offset, a value = tvb_get_bits64(out_tvb, 0, length, ENC_BIG_ENDIAN); } proto_item_append_text(actx->created_item, ", %s decimal value %" G_GINT64_MODIFIER "u", - decode_bits_in_field(0, length, value, ENC_BIG_ENDIAN), value); + decode_bits_in_field(actx->pinfo->pool, 0, length, value, ENC_BIG_ENDIAN), value); if (named_bits) { const guint32 named_bits_bytelen = (num_named_bits + 7) / 8; proto_tree *subtree = proto_item_add_subtree(actx->created_item, ett_per_named_bits); diff --git a/epan/proto.c b/epan/proto.c index f3c9fac677..ffce82634a 100644 --- a/epan/proto.c +++ b/epan/proto.c @@ -12309,7 +12309,7 @@ _proto_tree_add_bits_ret_val(proto_tree *tree, const int hfindex, tvbuff_t *tvb, CHECK_FOR_NULL_TREE(tree); TRY_TO_FAKE_THIS_ITEM(tree, hfindex, hf_field); - bf_str = decode_bits_in_field(bit_offset, no_of_bits, value, encoding); + bf_str = decode_bits_in_field(PNODE_POOL(tree), bit_offset, no_of_bits, value, encoding); switch (hf_field->type) { case FT_BOOLEAN: @@ -12568,13 +12568,22 @@ proto_tree_add_split_bits_crumb(proto_tree *tree, const int hfindex, tvbuff_t *t const crumb_spec_t *crumb_spec, guint16 crumb_index) { header_field_info *hfinfo; + gint start = bit_offset >> 3; + gint length = ((bit_offset + crumb_spec[crumb_index].crumb_bit_length - 1) >> 3) - (bit_offset >> 3) + 1; + + /* We have to duplicate this length check from proto_tree_add_text_internal in order to check for a null tree + * so that we can use the tree's memory scope in calculating the string */ + if (length == -1) { + tvb_captured_length(tvb) ? tvb_ensure_captured_length_remaining(tvb, start) : 0; + } else { + tvb_ensure_bytes_exist(tvb, start, length); + } + if (!tree) return; PROTO_REGISTRAR_GET_NTH(hfindex, hfinfo); - proto_tree_add_text_internal(tree, tvb, - bit_offset >> 3, - ((bit_offset + crumb_spec[crumb_index].crumb_bit_length - 1) >> 3) - (bit_offset >> 3) + 1, + proto_tree_add_text_internal(tree, tvb, start, length, "%s crumb %d of %s (decoded above)", - decode_bits_in_field(bit_offset, crumb_spec[crumb_index].crumb_bit_length, + decode_bits_in_field(PNODE_POOL(tree), bit_offset, crumb_spec[crumb_index].crumb_bit_length, tvb_get_bits(tvb, bit_offset, crumb_spec[crumb_index].crumb_bit_length, @@ -12648,7 +12657,7 @@ _proto_tree_add_bits_format_value(proto_tree *tree, const int hfindex, return NULL; } - str = decode_bits_in_field(bit_offset, no_of_bits, value, encoding); + str = decode_bits_in_field(PNODE_POOL(tree), bit_offset, no_of_bits, value, encoding); (void) g_strlcat(str, " = ", 256+64); (void) g_strlcat(str, hf_field->name, 256+64); diff --git a/epan/to_str.c b/epan/to_str.c index 8f260cc392..ce3a0231c8 100644 --- a/epan/to_str.c +++ b/epan/to_str.c @@ -645,7 +645,7 @@ rel_time_to_secs_str(wmem_allocator_t *scope, const nstime_t *rel_time) */ char * -decode_bits_in_field(const guint bit_offset, const gint no_of_bits, const guint64 value, const guint encoding) +decode_bits_in_field(wmem_allocator_t *scope, const guint bit_offset, const gint no_of_bits, const guint64 value, const guint encoding) { guint64 mask; char *str; @@ -667,7 +667,7 @@ decode_bits_in_field(const guint bit_offset, const gint no_of_bits, const guint6 } /* Prepare the string, 256 pos for the bits and zero termination, + 64 for the spaces */ - str=(char *)wmem_alloc0(wmem_packet_scope(), 256+64); + str=(char *)wmem_alloc0(scope, 256+64); for(bit=0;bit