Wireshark-dev: [Wireshark-dev] Should we be dynamically allocating the hash buf below?

From: Richard Sharpe <realrichardsharpe@xxxxxxxxx>
Date: Fri, 14 May 2021 19:51:05 -0700
Hi folks,

I notice that in the ieee1905 dissector there is now the following
code for calculating fragment hashes.

static guint
ieee1905_fragment_hash(gconstpointer k)
{
  guint hash_val;
  const ieee1905_fragment_key *key = (const ieee1905_fragment_key *)k;

  if (!key || !key->src.data || !key->dst.data) {
      return 0;
  }

  const guint8 src_len = key->src.len;
  const guint8 dst_len = key->dst.len;
  const guint8 hash_buf_len = src_len + dst_len + sizeof(guint8) +
sizeof(guint32);
  guint8* hash_buf = (guint8*)wmem_alloc(wmem_packet_scope(), hash_buf_len);

  memcpy(hash_buf, key->src.data, src_len);
  memcpy(&hash_buf[src_len], key->dst.data, dst_len);
  hash_buf[src_len + dst_len] = key->frag_id;
  memcpy(&hash_buf[src_len + dst_len + sizeof(guint8)], &key->vlan_id,
sizeof(guint32));
  hash_val = wmem_strong_hash((const guint8 *)hash_buf, hash_buf_len);
  return hash_val;
}

My question is: Should we be using wmem_alloc for the hash_buf because
it seems like we never use it after the hash is calculated so why
leave it around and why allocate it instead of using a buffer on the
stack.

We also never free it, although it will be freed when the packet goes
away, I guess.

-- 
Regards,
Richard Sharpe
(何以解憂?唯有杜康。--曹操)(传说杜康是酒的发明者)