Skip to content

Commit c056a99

Browse files
Ivan Morettmeta-codesync[bot]
authored andcommitted
Strip trailing colon from convertMacToString output
Summary: Fixes #258. MacHelpers.cpp:52 formatted each MAC byte as "{0:02x}:" unconditionally, so convertMacToString appended a colon after every byte including the last, producing "aa:bb:cc:dd:ee:ff:" instead of valid MAC notation "aa:bb:cc:dd:ee:ff". Rewrote the loop to insert the separator only between bytes (index > 0) instead of building the full string then truncating, dropping the now-unneeded intermediate locals. Checked every caller of convertMacToString (KatranGrpcService.cpp, KatranServiceHandler.cpp Thrift getMac handler) through to their consumers (katranadm CLI print, go client log line): both are display-only, nothing parses or string-compares the trailing colon, so blast radius is limited to the returned string's format. Updated MacHelpersTest.cpp's 3 hardcoded assertions and the stringToUintRoundTrip test, which previously codified the trailing colon (and worked around it with a manual substr) as expected behavior. Community PR #259 proposes the same fix via mac_string.substr(0, mac_string.size() - 1) (build-full-then-truncate); this diff takes the equivalent insert-separator-only approach instead to avoid the redundant allocation and unused intermediate locals, and additionally fixes the test fixtures the PR's description doesn't mention. Reviewed By: tagrawal03 Differential Revision: D114403491 fbshipit-source-id: 4d2fd4a5b60121d22644e7498f4e6fc820760434
1 parent dbbe81e commit c056a99

2 files changed

Lines changed: 12 additions & 15 deletions

File tree

katran/lib/MacHelpers.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ std::string convertMacToString(std::vector<uint8_t> mac) {
4444
if (mac.size() != 6) {
4545
return "unknown";
4646
}
47-
uint16_t mac_part;
48-
std::string mac_part_string;
4947
std::string mac_string;
50-
for (auto m : mac) {
51-
mac_part = m;
52-
mac_part_string = fmt::format("{0:02x}:", mac_part);
53-
mac_string += mac_part_string;
48+
for (size_t i = 0; i < mac.size(); i++) {
49+
if (i > 0) {
50+
mac_string += ":";
51+
}
52+
mac_string += fmt::format("{0:02x}", static_cast<uint16_t>(mac[i]));
5453
}
5554
return mac_string;
5655
}

katran/lib/tests/MacHelpersTest.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,17 @@ TEST(MacHelpersTests, convertMacToUintTooShortReturnsZeroes) {
5050

5151
TEST(MacHelpersTests, convertMacToStringValid) {
5252
const std::vector<uint8_t> mac{0x00, 0x11, 0x22, 0xaa, 0xbb, 0xcc};
53-
EXPECT_EQ(convertMacToString(mac), "00:11:22:aa:bb:cc:");
53+
EXPECT_EQ(convertMacToString(mac), "00:11:22:aa:bb:cc");
5454
}
5555

5656
TEST(MacHelpersTests, convertMacToStringAllZeroes) {
5757
const std::vector<uint8_t> mac(6, 0);
58-
EXPECT_EQ(convertMacToString(mac), "00:00:00:00:00:00:");
58+
EXPECT_EQ(convertMacToString(mac), "00:00:00:00:00:00");
5959
}
6060

6161
TEST(MacHelpersTests, convertMacToStringAllMax) {
6262
const std::vector<uint8_t> mac(6, 0xff);
63-
EXPECT_EQ(convertMacToString(mac), "ff:ff:ff:ff:ff:ff:");
63+
EXPECT_EQ(convertMacToString(mac), "ff:ff:ff:ff:ff:ff");
6464
}
6565

6666
TEST(MacHelpersTests, convertMacToStringWrongSizeReturnsUnknown) {
@@ -75,15 +75,13 @@ TEST(MacHelpersTests, convertMacToStringEmptyReturnsUnknown) {
7575
}
7676

7777
TEST(MacHelpersTests, stringToUintRoundTrip) {
78-
// The string->uint direction is reversible (modulo the trailing colon
79-
// that convertMacToString appends). Verifying the byte vector survives
80-
// a string->uint->string->uint round-trip catches any silent corruption.
78+
// Verifies the byte vector survives a string->uint->string->uint
79+
// round-trip, catching any silent corruption.
8180
const std::string input = "01:23:45:67:89:ab";
8281
const auto bytes = convertMacToUint(input);
8382
const auto reformatted = convertMacToString(bytes);
84-
EXPECT_EQ(reformatted, input + ":");
85-
EXPECT_EQ(
86-
convertMacToUint(reformatted.substr(0, reformatted.size() - 1)), bytes);
83+
EXPECT_EQ(reformatted, input);
84+
EXPECT_EQ(convertMacToUint(reformatted), bytes);
8785
}
8886

8987
} // namespace katran

0 commit comments

Comments
 (0)