Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: SocketAddress.cpp
- Revision:
- 25:68232387bc75
- Parent:
- 23:b3c679f20d13
- Child:
- 28:90d8f662de83
--- a/SocketAddress.cpp Tue Apr 19 18:24:34 2016 -0500 +++ b/SocketAddress.cpp Tue Apr 19 18:24:46 2016 -0500 @@ -80,11 +80,20 @@ static void ipv6_to_address(char *addr, const uint8_t *bytes) { + int pos = 0; for (int i = 0; i < NSAPI_IPv6_BYTES; i+=2) { - sprintf(&addr[5*i], "%02x%02x", bytes[i], bytes[i+1]); - addr[5*i+4] = ':'; + int ret = sprintf(&addr[pos], "%02x%02x", bytes[i], bytes[i+1]); + if (ret < 0) { + memset(addr, 0, NSAPI_IPv6_SIZE + 1); + return; + } + pos += ret; + + addr[pos++] = ':'; } - addr[NSAPI_IPv6_BYTES-1] = '\0'; + pos -= 1; // Overwrite last ':' + addr[pos++] = '\0'; + MBED_ASSERT(NSAPI_IPv6_SIZE == pos); } SocketAddress::SocketAddress(NetworkInterface *iface, const char *host, uint16_t port) @@ -136,7 +145,7 @@ address_to_ipv4(_ip_bytes, addr); } else if (addr && address_is_ipv6(addr)) { _ip_version = NSAPI_IPv6; - address_to_ipv4(_ip_bytes, addr); + address_to_ipv6(_ip_bytes, addr); } else { _ip_version = NSAPI_IPv4; memset(_ip_bytes, 0, NSAPI_IPv4_BYTES); @@ -171,7 +180,7 @@ if (!ip_address[0]) { if (_ip_version == NSAPI_IPv4) { ipv4_to_address(ip_address, _ip_bytes); - } else if (_ip_version == NSAPI_IPv4) { + } else if (_ip_version == NSAPI_IPv6) { ipv6_to_address(ip_address, _ip_bytes); } }