In software provided for multiple system on a chip microcontrollers I discovered this piece of code
String getMacAddress() {
uint8_t baseMac[6];
char baseMacChr[13] = {0};
# if defined(ESP8266)
WiFi.macAddress(baseMac);
sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]);
# elif defined(ESP32)
esp_read_mac(baseMac, ESP_MAC_WIFI_STA);
sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]);
# else
sprintf(baseMacChr, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
# endif
return String(baseMacChr);
}
Where String
is provided by Arduino context. So on those microcontroller is this piece of code safe?
I know that there is a problem with the scope/longevity of the returned variable (well explained here: Returning a C string from a function ) which most people get around with the std::string
but I don't have any experience in the context of microcontrollers, and since I've seen it used a lot, I've come up with this question.
I haven't found any documentation about it, every bit of extra information is welcome.