Skip to content

Variant::convert does not function correctly with null pointers if types don't match exactly #59

@rovarma

Description

@rovarma

Consider the following code:

class Base
{
};

class Derived : public Base
{
};

class SomeOtherClass
{
     Derived* mPointerProperty;
};

SomeOtherClass* object = new SomeOtherClass();
object->mPointerProperty = nullptr;

variant value = object->get_type().get_property("mPointerProperty").get_value(object);

Now, suppose we want to retrieve the pointer value from the variant and store it in a base pointer. We can do two things:

Base* pointer_value = value.get_value<Base*>(); // Works because Derived is derived from Base

This works because Derived is derived from Base. Or we can do:

bool converted_ok = false;
Base* pointer_value = value.convert<Base*>(&converted_ok);

This doesn't work; garbage is returned and converted_ok remains false, which is unexpected. The reason for this appears to be that variant::convert does not correctly deal with inheritance hierarchies when the value is a nullptr; when faced with this case, it goes through variant::try_pointer_conversion, which calls type::apply_offset, which returns a nullptr (because ptr is null), causing the function to fail.

I'm not sure how to fix it this time :)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions