<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/134902>134902</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-tidy] Check request: bugprone-host-to-network-after-memcpy
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
denzor200
</td>
</tr>
</table>
<pre>
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};
}
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVM2O2zYQfprxZaAFNZQl-6CDvI5PbdFDD70tKImWWEukQ45iOE9fUJZjb7BpE4OAQfDjfD8cjQrBdFbrEtY7WO9XauLe-bLV9qvzJMSqdu21BFH9oXUbUGHT6-aE3CvGixkGPBrbosLAXtlO4xRUp9EdUSEQ9S4wskOr-eL8CYjwONmGjbNAm8GcNAIVPTs7ABWobHvfB6ACaIvqyNqjYaAioPLdNGrLeFEBG3c2usWjdyMq9OqC9eCaU-Qe9ej89QVE9VdvwiJ5VhumrtM3TU0_C56d3EWFeABEi964ixai7lo3agoajUXuNY7RWaOCDsiRwwS8xErx7Oxd59U4ah_QazUMV7woy1EPiOrT39Xvf_72CWTcQC6WJSo1scOz8kEDbRpnA2PgNuJkVV9ZA1V4Vs1JM9Dr4yiYr_qNMf7FwKDYgagQ8YGYjGVJb4yNc6e3vvUIco8C5IfINI_V9Gc7jc-4BWqOCLSJZAjydWZ1x1nvrTTQNq4bOP649-4SM22UBSr47pAW-m_cox6b8xVoA5Q_qr0-W_4B2Tcf7_wtXfUOLHc3UUAHoAPuqj0mODh3CvcGjjxXN-GolY29aNn1c2-CPMR1I7ppQqBdZPpQ1yPcmFXy37j_Cff2Gr8c7fPLPqd7L_dhtu-4HnUe_bB8nU_Ae6q_HGz42WCfuX4c63eoBeg1T94-kjgr46HYPbfYcrHYLxeL_Xef5qotZbuVW7XSZVpkWSqLNMtXfZk3x1y1uWplmtJ2U7eyWWeFzDK1ETLNtytTkqC1yMQm3Uii_CXLhUhbSaptxbZeK8iEHpUZXobhy_jifLcyIUy6TGW2FbQaVK2HME9nomZQtkvYtNf4yOv9ypfxVlJPXYBMDCZweNRhw8M815-urff4Ok9Drz9POjDICuupO3tndRJHXcIuWaZfMs_e5NY5q8kPZc98DjHF-ZU7w_1UvzRuBDpE1uUvOXv3j24Y6DBbCUCHxc2Xkv4NAAD__2g1-Ds">