dynamic_cast in ConnFactory results in segfault

Hi,

I'm creating a new transport plugin, but when I try to use it I get a segfault
during the createAndCheckStream function of the ConnFactory:

During the size_hint calculation the ConnFactory does a dynamic_cast to a
TypeMarshaller, but my tranporter directly inherits from TypeTransporter and
not from TypeMarshaller, resulting in a null pointer after the dynamic_cast
and the segfault

Since I have no idea how to change the code (TypeTranporter does not have the
getSampleSize function) I just commented it out which removes the segfault:

diff --git a/rtt/internal/ConnFactory.cpp b/rtt/internal/ConnFactory.cpp
index c3e7289..b906812 100644
--- a/rtt/internal/ConnFactory.cpp
+++ b/rtt/internal/ConnFactory.cpp
@@ -130,6 +130,7 @@ bool
ConnFactory::createAndCheckStream(base::OutputPortInterface& output_port
log(Error) << "No such transport registered. Check your
policy.transport settings or add
return false;
}
+ /*
types::TypeMarshaller* ttt = dynamic_cast<types::TypeMarshaller*> ( type-
>getProtocol(policy
if (ttt) {
int size_hint = ttt->getSampleSize( output_port.getDataSource() );
@@ -137,6 +138,7 @@ bool
ConnFactory::createAndCheckStream(base::OutputPortInterface& output_port
} else {
log(Warning) <<"Could not determine sample size for type " << type-
>getTypeName() << end
}
+ */
RTT::base::ChannelElementBase::shared_ptr chan_stream = type-
>getProtocol(policy.transport)-

if ( !chan_stream ) {
~

Ruben

dynamic_cast in ConnFactory results in segfault

On Thu, Jul 22, 2010 at 2:09 PM, Ruben Smits
<ruben [dot] smits [..] ...> wrote:
>  Hi,
>
> I'm creating a new transport plugin, but when I try to use it I get a segfault
> during the createAndCheckStream function of the ConnFactory:
>
> During the size_hint calculation the ConnFactory does a dynamic_cast to a
> TypeMarshaller, but my tranporter directly inherits from TypeTransporter and
> not from TypeMarshaller, resulting in a null pointer after the dynamic_cast
> and the segfault

But the null pointer is stored in 'ttt' and I check directly after if
'ttt' is null or not.
So how can this code be ever called and cause a crash ?
You should only see 'Could not determine sample size for type...'

The mistake you mention is coincidentally made in the
'createOutOfBandConnection()', where there is a dynamic_cast without
checking the result (line 216).
I'll fix that one already.

Peter

>
> Since I have no idea how to change the code (TypeTranporter does not have the
> getSampleSize function) I just commented it out which removes the segfault:
>
> diff --git a/rtt/internal/ConnFactory.cpp b/rtt/internal/ConnFactory.cpp
> index c3e7289..b906812 100644
> --- a/rtt/internal/ConnFactory.cpp
> +++ b/rtt/internal/ConnFactory.cpp
> @@ -130,6 +130,7 @@ bool
> ConnFactory::createAndCheckStream(base::OutputPortInterface& output_port
>         log(Error) << "No such transport registered. Check your
> policy.transport settings or add
>         return false;
>     }
> +    /*
>     types::TypeMarshaller* ttt = dynamic_cast<types::TypeMarshaller*> ( type-
>>getProtocol(policy
>     if (ttt) {
>         int size_hint = ttt->getSampleSize( output_port.getDataSource() );
> @@ -137,6 +138,7 @@ bool
> ConnFactory::createAndCheckStream(base::OutputPortInterface& output_port
>     } else {
>         log(Warning) <<"Could not determine sample size for type " << type-
>>getTypeName() << end
>     }
> +    */
>     RTT::base::ChannelElementBase::shared_ptr chan_stream = type-
>>getProtocol(policy.transport)-
>
>     if ( !chan_stream ) {
> ~
>
>
> Ruben
> --
> Orocos-Dev mailing list
> Orocos-Dev [..] ...
> http://lists.mech.kuleuven.be/mailman/listinfo/orocos-dev
>
--
Orocos-Dev mailing list
Orocos-Dev [..] ...
http://lists.mech.kuleuven.be/mailman/listinfo/orocos-dev

Ruben Smits's picture

dynamic_cast in ConnFactory results in segfault

On Thursday 22 July 2010 17:08:47 Peter Soetens wrote:
> On Thu, Jul 22, 2010 at 2:09 PM, Ruben Smits
>
> <ruben [dot] smits [..] ...> wrote:
> > Hi,
> >
> > I'm creating a new transport plugin, but when I try to use it I get a
> > segfault during the createAndCheckStream function of the ConnFactory:
> >
> > During the size_hint calculation the ConnFactory does a dynamic_cast to a
> > TypeMarshaller, but my tranporter directly inherits from TypeTransporter
> > and not from TypeMarshaller, resulting in a null pointer after the
> > dynamic_cast and the segfault
>
> But the null pointer is stored in 'ttt' and I check directly after if
> 'ttt' is null or not.
> So how can this code be ever called and cause a crash ?
> You should only see 'Could not determine sample size for type...'
>
> The mistake you mention is coincidentally made in the
> 'createOutOfBandConnection()', where there is a dynamic_cast without
> checking the result (line 216).
> I'll fix that one already.

You're right, I was pointing to the wrong code (it looks so similar ;) ), it
was actually the createOutOfBandConnection that gave me the segfault.

Ruben

> Peter
>
> > Since I have no idea how to change the code (TypeTranporter does not have
> > the getSampleSize function) I just commented it out which removes the
> > segfault:
> >
> > diff --git a/rtt/internal/ConnFactory.cpp b/rtt/internal/ConnFactory.cpp
> > index c3e7289..b906812 100644
> > --- a/rtt/internal/ConnFactory.cpp
> > +++ b/rtt/internal/ConnFactory.cpp
> > @@ -130,6 +130,7 @@ bool
> > ConnFactory::createAndCheckStream(base::OutputPortInterface& output_port
> > log(Error) << "No such transport registered. Check your
> > policy.transport settings or add
> > return false;
> > }
> > + /*
> > types::TypeMarshaller* ttt = dynamic_cast<types::TypeMarshaller*> (
> > type-
> >
> >>getProtocol(policy
> >>
> > if (ttt) {
> > int size_hint = ttt->getSampleSize( output_port.getDataSource()
> > ); @@ -137,6 +138,7 @@ bool
> > ConnFactory::createAndCheckStream(base::OutputPortInterface& output_port
> > } else {
> > log(Warning) <<"Could not determine sample size for type " <<
> > type-
> >
> >>getTypeName() << end
> >>
> > }
> > + */
> > RTT::base::ChannelElementBase::shared_ptr chan_stream = type-
> >
> >>getProtocol(policy.transport)-
> >>
> > if ( !chan_stream ) {
> > ~
> >
> >
> > Ruben
> > --
> > Orocos-Dev mailing list
> > Orocos-Dev [..] ...
> > http://lists.mech.kuleuven.be/mailman/listinfo/orocos-dev