Skip to content

azmq::message's move assignment operator leaks memory. #144

Open
@JenSte

Description

@JenSte

The move assignment operator of azmq::message is broken:

  • Replaces the held zmq_msg_t by the one from the assigned object (msg_ = rhs.msg_;) without freeing the original one.
  • Does not take self-assignment into account, the own msg_ is erroneously re-initialized by calling zmq_msg_init(&rhs.msg_);.

The following program demonstrates the problems:

#include <azmq/message.hpp>
#include <iostream>

int main()
{
    int major, minor, patch;
    zmq_version(&major, &minor, &patch);

    std::cout << "boost version: " << BOOST_LIB_VERSION << '\n'
              << "zmq version: " << major << '.' << minor << '.' << patch << '\n'
              << "azmq version: git-a8f54cc8\n"
              << "gcc version: " << __GNUC__ << '.' << __GNUC_MINOR__ << '.' << __GNUC_PATCHLEVEL__ << std::endl;

    azmq::message m1(50);
    azmq::message m2(60);

    // Internal message of 'm2' is not freed.
    m2 = std::move(m1);


    azmq::message m3(70);

    std::cout << "m3.data() = " << m3.data() << '\n'
              << "m3.size() = " << m3.size() << std::endl;

    // Self-assignment, original (70 bytes long) buffer lost.
    m3 = std::move(m3);

    std::cout << "m3.data() = " << m3.data() << '\n'
              << "m3.size() = " << m3.size() << std::endl;
}

Output:

boost version: 1_63
zmq version: 4.1.6
azmq version: git-a8f54cc8
gcc version: 7.3.1
m3.data() = 0x60b000000118
m3.size() = 70
m3.data() = 0x7ffcd071dad0
m3.size() = 0

=================================================================
==3499==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 210 byte(s) in 2 object(s) allocated from:
    #0 0x7f15e0a66850 in malloc (/lib64/libasan.so.4+0xde850)
    #1 0x7f15e022d1bf  (/lib64/libzmq.so.5+0x261bf)

SUMMARY: AddressSanitizer: 210 byte(s) leaked in 2 allocation(s).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions