From f10fdf7ad4a88fea319a1a271c85a375c3370497 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 29 May 2015 11:01:03 -0600 Subject: [PATCH] Networking: Add a test to see a device pointer is still valid --- net/devif/devif_callback.c | 26 +++++++++++++++++++++++--- net/netdev/Make.defs | 4 ++-- net/netdev/netdev.h | 14 ++++++++++++++ net/netdev/netdev_findbyname.c | 4 ++-- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/net/devif/devif_callback.c b/net/devif/devif_callback.c index 11f1bdfb6b..997d81ac70 100644 --- a/net/devif/devif_callback.c +++ b/net/devif/devif_callback.c @@ -49,6 +49,7 @@ #include #include +#include "netdev/netdev.h" #include "devif/devif.h" /**************************************************************************** @@ -123,6 +124,16 @@ FAR struct devif_callback_s * if (dev) { + /* Verify that the device is valid */ + + if (!netdev_verify(dev)) + { + /* No.. release the callback structure and fail */ + + devif_callback_free(NULL, NULL, list); + return NULL; + } + ret->nxtdev = dev->d_devcb; dev->d_devcb = ret; } @@ -181,10 +192,15 @@ void devif_callback_free(FAR struct net_driver_s *dev, } #endif - /* Find the callback structure in the device event list */ + /* Remove the callback structure from the device notification list if + * it is supposed to be in the device notification list AND if the + * device pointer is still valid. + */ - if (dev) + if (dev && netdev_verify(dev)) { + /* Find the callback structure in the device event list */ + for (prev = NULL, curr = dev->d_devcb; curr && curr != cb; prev = curr, curr = curr->nxtdev); @@ -205,10 +221,14 @@ void devif_callback_free(FAR struct net_driver_s *dev, } } - /* Find the callback structure in the connection event list */ + /* Remove the callback structure from the data notification list if + * it is supposed to be in the data notification list. + */ if (list) { + /* Find the callback structure in the connection event list */ + for (prev = NULL, curr = *list; curr && curr != cb; prev = curr, curr = curr->nxtconn); diff --git a/net/netdev/Make.defs b/net/netdev/Make.defs index 50fffc8bbc..40c08f9642 100644 --- a/net/netdev/Make.defs +++ b/net/netdev/Make.defs @@ -1,7 +1,7 @@ ############################################################################ # net/netdev/Make.defs # -# Copyright (C) 2014 Gregory Nutt. All rights reserved. +# Copyright (C) 2014-2015 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -38,7 +38,7 @@ NETDEV_CSRCS += netdev_register.c netdev_ioctl.c netdev_txnotify.c NETDEV_CSRCS += netdev_findbyname.c netdev_findbyaddr.c netdev_count.c NETDEV_CSRCS += netdev_foreach.c netdev_unregister.c netdev_sem.c -NETDEV_CSRCS += netdev_carrier.c netdev_default.c +NETDEV_CSRCS += netdev_carrier.c netdev_default.c netdev_verify.c ifeq ($(CONFIG_NET_RXAVAIL),y) NETDEV_CSRCS += netdev_rxnotify.c diff --git a/net/netdev/netdev.h b/net/netdev/netdev.h index 5377c2d2d8..8c9bd1077d 100644 --- a/net/netdev/netdev.h +++ b/net/netdev/netdev.h @@ -43,6 +43,7 @@ #include #include +#include #include @@ -124,6 +125,19 @@ void netdev_semgive(void); void netdev_ifup(FAR struct net_driver_s *dev); void netdev_ifdown(FAR struct net_driver_s *dev); +/**************************************************************************** + * Function: netdev_verify + * + * Description: + * Verify that the specified device still exists + * + * Assumptions: + * The caller has locked the network. + * + ****************************************************************************/ + +bool netdev_verify(FAR struct net_driver_s *dev); + /**************************************************************************** * Function: netdev_findbyname * diff --git a/net/netdev/netdev_findbyname.c b/net/netdev/netdev_findbyname.c index e428872d8a..d9d44a9701 100644 --- a/net/netdev/netdev_findbyname.c +++ b/net/netdev/netdev_findbyname.c @@ -89,9 +89,9 @@ * ****************************************************************************/ -FAR struct net_driver_s *netdev_findbyname(const char *ifname) +FAR struct net_driver_s *netdev_findbyname(FAR const char *ifname) { - struct net_driver_s *dev; + FAR struct net_driver_s *dev; if (ifname) { netdev_semtake();