Skip to content

network: do not print an invalid (0-length) address

Tommaso Pecorella requested to merge tommypec/ns-3-dev:address-print into master

The current Address printing function contains the following code:

    os << std::setw(2) << (uint32_t)address.m_type << "-" << std::setw(2) << (uint32_t)address.m_len
       << "-";
    for (uint8_t i = 0; i < (address.m_len - 1); ++i)
    {
        os << std::setw(2) << (uint32_t)address.m_data[i] << ":";
    }
    // Final byte not suffixed by ":"
    os << std::setw(2) << (uint32_t)address.m_data[address.m_len - 1];

The problem is: what happens when an Address is invalid (i.e., m_len is zero)?

Actually it prints 00-00-00, but this is both confusing and very brittle. As a matter of fact, it relies on address.m_len - 1 being evaluated to a signed integer. If it doesn't and the type is kept, it will wrap and will lead to 255, which in turns will lead to a memory error, as address.m_data is only 20 bytes long.

I hate this kind of hidden promotions. And writing nothing makes more sense than printing 00-00.

Merge request reports