Skip to content

Commit b3fec39

Browse files
committed
Update toString function with single buf and return
1 parent 72e39b8 commit b3fec39

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

cores/esp32/MacAddress.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,19 @@ int MacAddress::toString(char *buf) {
125125
}
126126

127127
String MacAddress::toString() const {
128+
uint8_t bytes = (_type == MAC6) ? 6 : 8;
129+
char buf[bytes];
128130
if(_type == MAC6) {
129-
char buf[18];
130-
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
131+
snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X",
131132
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
132133
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5]);
133-
return String(buf);
134134
} else {
135-
char buf[24];
136-
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
135+
snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
137136
_mac.bytes[0], _mac.bytes[1], _mac.bytes[2],
138137
_mac.bytes[3], _mac.bytes[4], _mac.bytes[5],
139138
_mac.bytes[6], _mac.bytes[7]);
140-
return String(buf);
141139
}
140+
return String(buf);
142141
}
143142

144143
uint64_t MacAddress::Value() {

0 commit comments

Comments
 (0)