Skip to content

msgpack::clone() never creates a new zone #763

@thpica

Description

@thpica

While using msgpack::clone() I had pretty bad issues (std::bad_cast) when cloning any object that would be allocated to the zone.

I think I figured why:

  1. When creating a aligned_zone_size_visitor, s will be copied to a private property m_size:

    struct aligned_zone_size_visitor {
        explicit aligned_zone_size_visitor(std::size_t s)
            :m_size(s) {}
        ...
    private:
        std::size_t m_size;
    };   

    https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/v1/object.hpp#L529-L600

  2. Because the original reference to s is not retained, s will stay at its initial value and msgpack::aligned_zone_size() always returns 0:

    inline std::size_t aligned_zone_size(msgpack::object const& obj) {
        std::size_t s = 0;
        aligned_zone_size_visitor vis(s);
        msgpack::object_parser(obj).parse(vis);
        return s;
    }

    https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/v1/object.hpp#L602-L607

  3. msgpack::clone() only instantiate a new zone if msgpack::aligned_zone_size() > 0 so it will never create a new zone:

    /// clone object
    /**
     * Clone (deep copy) object.
     * The copied object is located on newly allocated zone.
     * @param obj copy source object
     *
     * @return object_handle that holds deep copied object and zone.
     */
    inline object_handle clone(msgpack::object const& obj) {
        std::size_t size = msgpack::aligned_zone_size(obj);
        msgpack::unique_ptr<msgpack::zone> z(size == 0 ? MSGPACK_NULLPTR : new msgpack::zone(size));
        msgpack::object newobj = z.get() ? msgpack::object(obj, *z) : obj;
        return object_handle(newobj, msgpack::move(z));
    }

    https://github.com/msgpack/msgpack-c/blob/master/include/msgpack/v1/object.hpp#L609-L622

Is this an actual bug or a feature I misunderstood?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions