From 0034abfa45abc9d6a3b4382627a02a2c88d2d385 Mon Sep 17 00:00:00 2001 From: David van Moolenbroek Date: Fri, 18 Nov 2016 22:25:48 +0000 Subject: [PATCH] Always check whether netif_default is NULL In general, netif_default may be NULL, and various places in the code already check for this case before attempting to dereference the netif_default pointer. Some places do not perform this check though, and may cause null pointer dereferences if netif_default is not set. This patch adds NULL checks to those places as well. --- src/core/ipv4/ip4.c | 2 +- src/core/ipv6/ip6.c | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/core/ipv4/ip4.c b/src/core/ipv4/ip4.c index c7836774..ceaeb7c6 100644 --- a/src/core/ipv4/ip4.c +++ b/src/core/ipv4/ip4.c @@ -177,7 +177,7 @@ ip4_route(const ip4_addr_t *dest) /* loopif is disabled, looopback traffic is passed through any netif */ if (ip4_addr_isloopback(dest)) { /* don't check for link on loopback traffic */ - if (netif_is_up(netif_default)) { + if (netif_default != NULL && netif_is_up(netif_default)) { return netif_default; } /* default netif is not up, just use any netif for loopback traffic */ diff --git a/src/core/ipv6/ip6.c b/src/core/ipv6/ip6.c index 8cc9c010..d645c55f 100644 --- a/src/core/ipv6/ip6.c +++ b/src/core/ipv6/ip6.c @@ -94,7 +94,8 @@ ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest) if (ip6_addr_islinklocal(dest)) { if (ip6_addr_isany(src)) { /* Use default netif, if Up. */ - if (!netif_is_up(netif_default) || !netif_is_link_up(netif_default)) { + if (netif_default == NULL || !netif_is_up(netif_default) || + !netif_is_link_up(netif_default)) { return NULL; } return netif_default; @@ -114,7 +115,8 @@ ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest) } /* netif not found, use default netif, if up */ - if (!netif_is_up(netif_default) || !netif_is_link_up(netif_default)) { + if (netif_default == NULL || !netif_is_up(netif_default) || + !netif_is_link_up(netif_default)) { return NULL; } return netif_default; @@ -172,7 +174,7 @@ ip6_route(const ip6_addr_t *src, const ip6_addr_t *dest) /* loopif is disabled, loopback traffic is passed through any netif */ if (ip6_addr_isloopback(dest)) { /* don't check for link on loopback traffic */ - if (netif_is_up(netif_default)) { + if (netif_default != NULL && netif_is_up(netif_default)) { return netif_default; } /* default netif is not up, just use any netif for loopback traffic */