[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ns] NS2 problem with Packet::datalen() function (packet.h)



> For anyone interested, in "packet.h", there is a method define "datalen()" :
>
> inline int Packet::datalen() const {return data_ ? data_->size() : 0}
>
> Since "data_" is actually a pointer of type AppData (the base class
> of PacketData), this actually ends up returning the sizeof the
> AppData class instead of the actual data_'s datalen_ ...

AppData is an abstract class, and its size() method is a virtual one so
type checking that you mentioned below is done automatically. Typecasting
it to 'PacketData' will ruin it.

- Haobo

> A fix for this is to cast the "data_" member before calling the
> virtual size() method:
>
> inline int Packet::datalen() const {return data_ ?
> ((PacketData*)data_)->size() : 0}
>
>
> (Note this assumption is that the "data_" member _is_ of type
> PACKET_DATA ... the datalen() function could be made safer with a
> check to verify the type is PACKET_DATA before accessing the size()
> method ...)
>
> This is at least the case on ns compiled with gcc-2.96 under Redhat Linux
>