Xtonlsab bug

Joakim Tjernlund joakim.tjernlund at transmode.se
Fri Apr 30 11:49:47 CEST 2010


Ondrej Zajicek <santiago at crfreenet.org> wrote on 2010/04/30 10:19:15:
>
> On Thu, Apr 29, 2010 at 11:57:32PM +0200, Joakim Tjernlund wrote:
> >
> > Ondrej Zajicek <santiago at crfreenet.org> wrote on 2010/04/29 23:15:22:
> > >
> > > On Thu, Apr 29, 2010 at 11:03:32PM +0200, Joakim Tjernlund wrote:
> > > >
> > > > Ondrej, this looks buggy:
> > > >
> > > > +static inline void htonlsab(void *h, void *n, u16 len) { memcpy(n, h, len); };
> > > > +static inline void ntohlsab(void *n, void *h, u16 len) { memcpy(h, n, len); };
> > > >
> > > > memcpy is not defined to handle overlapping memory. Best to add:
> > > > if (n != h)
> > > >   memcpy(...)
> > >
> > > Yes, but all usages of htonlsab()/ntohlsab() are non-overlapping.
> > > (overlapping/on place usage was replaced with htonlsab1()/ntohlsab1()).
> >
> > I see, but it would be safer to have that check in case you
> > used them in the wrong way?
>
> I think this is no issue. Even if someone used them in a wrong way
> (which is improbable, as these functions are used just in a few
> places), memcpy() would probably do nothing when src==dst (although
> it is unspecified).

Yes, it is unlikely but if that were to happen you will have
a very hard time finding the problem as the real cause would
not be visible and only on some platform/gcc version.

>
> > > > Might as well do that for these too:
> > > > +static inline void htonlsah(struct ospf_lsa_header *h, struct
> > > ospf_lsa_header *n) { *n = *h; };
> > > > +static inline void ntohlsah(struct ospf_lsa_header *n, struct
> > > ospf_lsa_header *h) { *h = *n; };
> > >
> > > *n = *h should be OK even if n == h, AFAIK.
> >
> > Hopyfully, but isn't gcc allowed to use memcpy to do that
> > assignment?
>
> Definitely isn't allowed to do something that breaks the specified
> behavior of C operators.

Sure, but I am not sure if pointer references are covered fully by C.

>
> > If you add that if (h!=n) test, doesn't the need for Xlasb1() go away as well?
>
> I think it is cleaner to have Xlsab1() for that purpose.

  Jocke




More information about the Bird-users mailing list