Huge thanks to our Platinum Members Endace and LiveAction,
and our Silver Member Veeam, for supporting the Wireshark Foundation and project.

Ethereal-dev: Re: [Ethereal-dev] TVBuff: tvb_memcpy

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Gilbert Ramirez <gilramir@xxxxxxxxx>
Date: Wed, 21 Mar 2001 12:11:03 -0600
Michal.Melerowicz@xxxxxxxxx wrote:
> 
> > Hi all,
> >
> > I have question regarding tvbuff:
> >
> > I've got very strange behaviour of tvbuffs (strange for me of course).
> > There is a structure (below) I defined. I'm trying to copy 118 bytes from
> > tvbuff to myvar (type of mystruct), and what I got:

> >
> > /* same result
> >           tvb_memcpy(tvb, (guint8*)&myvar, offset+3, 93);
> >           tvb_memcpy(tvb, ((guint8*)&myvar)+93, offset+93, 25);
> >
> >           myvar = (struct_t *)tvb_memdup(tvb, offset+3, 118);*/
> >
> > }

Even though Ethereal itself sets a bad example in some places,
you cannot portably copy an array of bytes onto a struct and expect
a one-to-one correlation. The compiler may not have packed the
fields in the struct next to each other --- there may be padding
between fields. The only thing the ANSI C spec guarantees is the
*order* of the fields in the struct, not their closeness.

GCC has a way of specifying a struct as being packed, but we
don't want to limit Ethereal to being compiled only by gcc.

We need to fix the occurences of this bad practice that already
exist in ethereal.

--gilbert