Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
rovarma opened this issue May 16, 2017 · 3 comments

Comments

@rovarma
Copy link

rovarma commented May 16, 2017

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 :)

@acki-m
Copy link
Contributor

acki-m commented May 16, 2017

Thanks for reporting this issue. It is fixable.
Actually, the function type::apply_offset needs to be adjusted, or I add a new one.

if (src_raw_type == tgt_raw_type || ptr == nullptr)
        return ptr;

The nullptr check needs to be removed and the registered conversion function (class_list.m_conversion_list[i](info.m_ptr);) needs be called, if the type is in the inheritance graph.
We cannot do a simply reinterpret_cast. The conversion functions contains the static_casts to the pointer types. Thats what we need to call.
Its not that much code, I need to see when I have time for this. At moment I working on issue 57

acki-m added a commit that referenced this issue May 17, 2017
…types

I.e. with nullptr's only a downcast is possible, not an upcast.

Fiexe #59
@acki-m
Copy link
Contributor

acki-m commented May 17, 2017

I added a fix, please check whether its working for you or not.
Remark: only a downcast to base classes is possible with nullptrs, not an upcast.

@acki-m acki-m closed this as completed May 17, 2017
@rovarma
Copy link
Author

rovarma commented May 18, 2017

Thanks!

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants