Skip to content

[clang-tidy] Check request: bugprone-memcpy-after-network-to-host #134901

@denzor200

Description

@denzor200

Needs a check that will find a strange usage of a "network to host" function(like ntohl and ntohs) before it's result was copied to a raw block of memory.
This check will suggest to change that functions to "host to network" versions because in the most cases this is what the programmers really want.

EXAMPLE:

void send(std::uint32_t cook_hdr, std::uint16_t seqnum) {
    std::byte packet[6];
    cook_hdr = ntohl(cook_hdr);      // BAD - looks strange, you mean 'htonl' ??
    std::memcpy(&packet[0], &cook_hdr, sizeof(cook_hdr));
    seqnum = ntohs(seqnum);          // BAD - looks strange, you mean 'htons' ??
    std::memcpy(&packet[4], &seqnum, sizeof(seqnum));
    send_raw(packet, sizeof(packet));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions