From f86cd02a946660381c16b3d3eaddc44f555c8f33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Ma=C5=82ecki?= Date: Sat, 27 Sep 2014 00:43:21 +0200 Subject: [PATCH] linux: return early if we have no interfaces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This was pointed out as possible undefined allocation of 0 bytes by clang-analyzer. `malloc` of 0 bytes can return either `NULL` or a valid pointer which can't be dereferenced. On systems which return `NULL`, we would consider it a `malloc` error and return an `ENOMEM` to the caller. PR-URL: https://github.com/libuv/libuv/pull/13 Reviewed-By: Ben Noordhuis Reviewed-By: Saúl Ibarra Corretgé --- src/unix/linux-core.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/unix/linux-core.c b/src/unix/linux-core.c index 9c9a2be6..a2145b0f 100644 --- a/src/unix/linux-core.c +++ b/src/unix/linux-core.c @@ -754,6 +754,7 @@ int uv_interface_addresses(uv_interface_address_t** addresses, return -errno; *count = 0; + *addresses = NULL; /* Count the number of interfaces */ for (ent = addrs; ent != NULL; ent = ent->ifa_next) { @@ -766,6 +767,9 @@ int uv_interface_addresses(uv_interface_address_t** addresses, (*count)++; } + if (*count == 0) + return 0; + *addresses = malloc(*count * sizeof(**addresses)); if (!(*addresses)) return -ENOMEM;