Issue 134902
Summary [clang-tidy] Check request: bugprone-host-to-network-after-memcpy
Labels clang-tidy
Assignees
Reporter denzor200
    
Needs a check that will find a strange usage of a "host to network" function(like 'htonl' and 'htons') after it's argument was copied from a raw block of memory.
This check will suggest to change that functions to "network to host" because in the most cases this is what the programmers really want.

EXAMPLE:
```
auto parse(const std::byte* packet, std::size_t size) {
    std::uint32_t cook_hdr = 0;
    std::uint16_t seqnum = 0;

    if (size < sizeof(cook_hdr))
        throw "can't parse";
 std::memcpy(&cook_hdr, packet, sizeof(cook_hdr));
    cook_hdr = htonl(cook_hdr);      // BAD - looks strange, you mean 'ntohl' ??
    packet += sizeof(cook_hdr);
    size -= sizeof(cook_hdr);

    if (size < sizeof(seqnum))
        throw "can't parse";
    std::memcpy(&seqnum, packet, sizeof(seqnum));
    seqnum = htons(seqnum);         // BAD - looks strange, you mean 'ntohs' ??
    packet += sizeof(seqnum);
    size -= sizeof(seqnum);

    return std::pair{cook_hdr, seqnum};
}
```

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to