From bd4711055433f588b16b1629c0b18e5696e24460 Mon Sep 17 00:00:00 2001 From: Max Payne Date: Mon, 2 Apr 2018 22:23:23 +0300 Subject: [PATCH] Fix IPv4 address parsing due to not-so-portable scanf modifier Bug is raised when using newlib-based toolchains. %hh format is only avaliable in scanf if newlib is compiled with _WANT_IO_C99_FORMATS option. --- features/netsocket/SocketAddress.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/features/netsocket/SocketAddress.cpp b/features/netsocket/SocketAddress.cpp index 8fc2595294..15a46a2f4b 100644 --- a/features/netsocket/SocketAddress.cpp +++ b/features/netsocket/SocketAddress.cpp @@ -66,13 +66,15 @@ static void ipv4_from_address(uint8_t *bytes, const char *addr) int i = 0; for (; count < NSAPI_IPv4_BYTES; count++) { - unsigned char b; - int scanned = sscanf(&addr[i], "%hhu", &b); + unsigned d; + // Not using %hh, since it might be missing in newlib-based toolchains. + // See also: https://git.io/vxiw5 + int scanned = sscanf(&addr[i], "%u", &d); if (scanned < 1) { return; } - bytes[count] = b; + bytes[count] = static_cast(d); for (; addr[i] != '.'; i++) { if (!addr[i]) {