Skip to content

Commit 5dd8b27

Browse files
authored
When assigning to uint16_t, doing uint16_t x = uint16_t(a) + uint16_t(b) is maybe not doing what one expects because uint16_t(a) + uint16_t(b) is promoted to int... and then you are left with an implicit cast back to uint16_t. (#34)
1 parent 20460a9 commit 5dd8b27

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/parser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ namespace ada::parser {
231231
// set value to value × 0x10 + c interpreted as hexadecimal number, and increase pointer and length by 1.
232232
while (length < 4 && unicode::is_ascii_hex_digit(*pointer)) {
233233
char temp[2] = {*pointer, 0};
234-
value = static_cast<uint16_t>(value * 0x10) + static_cast<uint16_t>(std::strtol(temp, nullptr, 16));
234+
// https://stackoverflow.com/questions/39060852/why-does-the-addition-of-two-shorts-return-an-int
235+
value = uint16_t(value * 0x10 + std::strtol(temp, nullptr, 16));
235236
pointer++;
236237
length++;
237238
}
@@ -304,7 +305,8 @@ namespace ada::parser {
304305
}
305306

306307
// Set address[pieceIndex] to address[pieceIndex] × 0x100 + ipv4Piece.
307-
address[piece_index] = static_cast<uint16_t>(address[piece_index] * 0x100) + *ipv4_piece;
308+
// https://stackoverflow.com/questions/39060852/why-does-the-addition-of-two-shorts-return-an-int
309+
address[piece_index] = uint16_t(address[piece_index] * 0x100 + *ipv4_piece);
308310

309311
// Increase numbersSeen by 1.
310312
numbers_seen++;

0 commit comments

Comments
 (0)