Skip to content

Commit eb14bb7

Browse files
committed
Remove sprintf from generateDefaultHostName
1 parent 1f6d782 commit eb14bb7

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/Ethernet.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,16 @@ void EthernetClass::setRetransmissionCount(uint8_t num)
233233

234234
void EthernetClass::generateDefaultHostName(uint8_t *mac) {
235235
// Generate a default host name based on the MAC address
236-
sprintf_P(_hostName, PSTR("%s%02X%02X%02X"), DEFAULT_HOST_NAME, mac[3], mac[4], mac[5]);
236+
strcpy_P(_hostName, PSTR(DEFAULT_HOST_NAME));
237+
238+
// Append last 3 bytes of MAC address to the name
239+
PGM_P hexChars = PSTR("0123456789ABCDEF");
240+
for (int i = 0; i <= 2; i++)
241+
{
242+
_hostName[DEFAULT_HOST_NAME_LENGTH + i * 2] = pgm_read_byte_near(hexChars + (mac[3 + i] >> 4));
243+
_hostName[DEFAULT_HOST_NAME_LENGTH + i * 2 + 1] = pgm_read_byte_near(hexChars + (mac[3 + i] & 0x0F));
244+
}
245+
_hostName[DEFAULT_HOST_NAME_LENGTH + 6] = '\0';
237246
}
238247

239248
void EthernetClass::setHostName(const char *dhcpHost) {

src/Ethernet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "Udp.h"
5555

5656
#define DEFAULT_HOST_NAME "WIZnet"
57+
#define DEFAULT_HOST_NAME_LENGTH (sizeof(DEFAULT_HOST_NAME) - 1)
5758
#define HOST_NAME_MAX_LEN 20 // Max 30 or change the DHCP local buffer size
5859

5960
enum EthernetLinkStatus {

0 commit comments

Comments
 (0)