This patch fixes a possible null pointer dereference in
batadv_gw_election, detected by the semantic patch
deref_null.cocci, with the following warning:

./net/batman-adv/gateway_client.c:289:15-24: ERROR: next_gw is NULL but 
dereferenced.
./net/batman-adv/gateway_client.c:290:15-29: ERROR: next_gw is NULL but 
dereferenced.
./net/batman-adv/gateway_client.c:291:15-29: ERROR: next_gw is NULL but 
dereferenced.
./net/batman-adv/gateway_client.c:292:15-27: ERROR: next_gw is NULL but 
dereferenced.
./net/batman-adv/gateway_client.c:293:15-27: ERROR: next_gw is NULL but 
dereferenced.

Signed-off-by: Wen Yang <wen.yan...@zte.com.cn>
Reviewed-by: Tan Hu <tan...@zte.com.cn>
---
 net/batman-adv/gateway_client.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/net/batman-adv/gateway_client.c b/net/batman-adv/gateway_client.c
index 140c61a..d80ef1c 100644
--- a/net/batman-adv/gateway_client.c
+++ b/net/batman-adv/gateway_client.c
@@ -284,14 +284,16 @@ void batadv_gw_election(struct batadv_priv *bat_priv)
                batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
                                    gw_addr);
        } else {
-               batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
-                          "Changing route to gateway %pM (bandwidth: 
%u.%u/%u.%u MBit, tq: %i)\n",
-                          next_gw->orig_node->orig,
-                          next_gw->bandwidth_down / 10,
-                          next_gw->bandwidth_down % 10,
-                          next_gw->bandwidth_up / 10,
-                          next_gw->bandwidth_up % 10,
-                          router_ifinfo->bat_iv.tq_avg);
+               if (next_gw) {
+                       batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
+                                  "Changing route to gateway %pM (bandwidth: 
%u.%u/%u.%u MBit, tq: %i)\n",
+                                   next_gw->orig_node->orig,
+                                   next_gw->bandwidth_down / 10,
+                                   next_gw->bandwidth_down % 10,
+                                   next_gw->bandwidth_up / 10,
+                                   next_gw->bandwidth_up % 10,
+                                   router_ifinfo->bat_iv.tq_avg);
+               }
                batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
                                    gw_addr);
        }
-- 
2.9.5

Reply via email to