nameservice: fix potential null pointer dereference warnings on gcc 7
authorFerry Huberts <ferry.huberts@pelagic.nl>
Tue, 21 Nov 2017 08:36:03 +0000 (09:36 +0100)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Tue, 21 Nov 2017 09:11:13 +0000 (10:11 +0100)
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
lib/nameservice/src/mapwrite.c
lib/nameservice/src/nameservice.c

index 907e788..8aba8d2 100644 (file)
@@ -83,7 +83,7 @@ lookup_position_latlon(union olsr_ip_addr *ip)
 
       entry = list2db(list_node);
 
-      if (entry->names && ipequal(&entry->originator, ip)) {
+      if (entry && entry->names && ipequal(&entry->originator, ip)) {
         return entry->names->name;
       }
     }
@@ -158,7 +158,7 @@ mapwrite_work(FILE * fmap)
 
       entry = list2db(list_node);
 
-      if (NULL != entry->names) {
+      if (entry && entry->names) {
         if (0 >
             fprintf(fmap, "Node('%s',%s,'%s','%s');\n", olsr_ip_to_string(&strbuf1, &entry->originator), entry->names->name,
                     olsr_ip_to_string(&strbuf2, &entry->names->ip), lookup_name_latlon(&entry->originator))) {
index 3244ac5..36b5969 100644 (file)
@@ -1121,36 +1121,37 @@ write_hosts_file(void)
     for (list_node = list_head->next; list_node != list_head; list_node = list_node->next) {
 
       entry = list2db(list_node);
+      if (entry) {
+        for (name = entry->names; name != NULL; name = name->next) {
+          struct ipaddr_str strbuf1, strbuf2;
+          OLSR_PRINTF(6, "%s\t%s%s\t#%s\n", olsr_ip_to_string(&strbuf1, &name->ip), name->name, my_suffix,
+                      olsr_ip_to_string(&strbuf2, &entry->originator));
 
-      for (name = entry->names; name != NULL; name = name->next) {
-        struct ipaddr_str strbuf1, strbuf2;
-        OLSR_PRINTF(6, "%s\t%s%s\t#%s\n", olsr_ip_to_string(&strbuf1, &name->ip), name->name, my_suffix,
-                    olsr_ip_to_string(&strbuf2, &entry->originator));
-
-        fprintf(hosts, "%s\t%s%s\t# %s\n", olsr_ip_to_string(&strbuf1, &name->ip), name->name, my_suffix,
-                olsr_ip_to_string(&strbuf2, &entry->originator));
+          fprintf(hosts, "%s\t%s%s\t# %s\n", olsr_ip_to_string(&strbuf1, &name->ip), name->name, my_suffix,
+                  olsr_ip_to_string(&strbuf2, &entry->originator));
 
 #ifdef MID_ENTRIES
-        // write mid entries
-        if ((alias = mid_lookup_aliases(&name->ip)) != NULL) {
-          unsigned short mid_num = 1;
-          char mid_prefix[MID_MAXLEN];
+          // write mid entries
+          if ((alias = mid_lookup_aliases(&name->ip)) != NULL) {
+            unsigned short mid_num = 1;
+            char mid_prefix[MID_MAXLEN];
 
-          while (alias != NULL) {
-            // generate mid prefix
-            sprintf(mid_prefix, MID_PREFIX, mid_num);
+            while (alias != NULL) {
+              // generate mid prefix
+              sprintf(mid_prefix, MID_PREFIX, mid_num);
 
-            OLSR_PRINTF(6, "%s\t%s%s%s\t# %s (mid #%i)\n", olsr_ip_to_string(&strbuf1, &alias->alias), mid_prefix, name->name,
-                        my_suffix, olsr_ip_to_string(&strbuf2, &entry->originator), mid_num);
+              OLSR_PRINTF(6, "%s\t%s%s%s\t# %s (mid #%i)\n", olsr_ip_to_string(&strbuf1, &alias->alias), mid_prefix, name->name,
+                          my_suffix, olsr_ip_to_string(&strbuf2, &entry->originator), mid_num);
 
-            fprintf(hosts, "%s\t%s%s%s\t# %s (mid #%i)\n", olsr_ip_to_string(&strbuf1, &alias->alias), mid_prefix, name->name,
-                    my_suffix, olsr_ip_to_string(&strbuf2, &entry->originator), mid_num);
+              fprintf(hosts, "%s\t%s%s%s\t# %s (mid #%i)\n", olsr_ip_to_string(&strbuf1, &alias->alias), mid_prefix, name->name,
+                      my_suffix, olsr_ip_to_string(&strbuf2, &entry->originator), mid_num);
 
-            alias = alias->next_alias;
-            mid_num++;
+              alias = alias->next_alias;
+              mid_num++;
+            }
           }
-        }
 #endif /* MID_ENTRIES */
+        }
       }
     }
   }
@@ -1222,14 +1223,15 @@ write_services_file(bool writemacs)
     for (list_node = list_head->next; list_node != list_head; list_node = list_node->next) {
 
       entry = list2db(list_node);
-
-      for (name = entry->names; name != NULL; name = name->next) {
-        struct ipaddr_str strbuf;
-        OLSR_PRINTF(6, "%s\t", name->name);
-        OLSR_PRINTF(6, "\t#%s\n", olsr_ip_to_string(&strbuf, &entry->originator));
-
-        fprintf(file, "%s\t", name->name);
-        fprintf(file, "\t#%s\n", olsr_ip_to_string(&strbuf, &entry->originator));
+      if (entry) {
+        for (name = entry->names; name != NULL; name = name->next) {
+          struct ipaddr_str strbuf;
+          OLSR_PRINTF(6, "%s\t", name->name);
+          OLSR_PRINTF(6, "\t#%s\n", olsr_ip_to_string(&strbuf, &entry->originator));
+
+          fprintf(file, "%s\t", name->name);
+          fprintf(file, "\t#%s\n", olsr_ip_to_string(&strbuf, &entry->originator));
+        }
       }
     }
   }
@@ -1329,27 +1331,28 @@ write_resolv_file(void)
     for (list_node = list_head->next; list_node != list_head; list_node = list_node->next) {
 
       entry = list2db(list_node);
-
-      for (name = entry->names; name != NULL; name = name->next) {
+      if (entry) {
+        for (name = entry->names; name != NULL; name = name->next) {
 #ifndef NODEBUG
-        struct ipaddr_str strbuf;
-        struct lqtextbuffer lqbuffer;
+          struct ipaddr_str strbuf;
+          struct lqtextbuffer lqbuffer;
 #endif /* NODEBUG */
-        route = olsr_lookup_routing_table(&name->ip);
+          route = olsr_lookup_routing_table(&name->ip);
 
-        OLSR_PRINTF(6, "NAME PLUGIN: check route for nameserver %s %s", olsr_ip_to_string(&strbuf, &name->ip),
-                    route ? "suceeded" : "failed");
+          OLSR_PRINTF(6, "NAME PLUGIN: check route for nameserver %s %s", olsr_ip_to_string(&strbuf, &name->ip),
+                      route ? "suceeded" : "failed");
 
-        if (route == NULL)      // it's possible that route is not present yet
-          continue;
+          if (route == NULL)      // it's possible that route is not present yet
+            continue;
 
-        /* enqueue it on the head of list */
-        *nameserver_routes = route;
-        OLSR_PRINTF(6, "NAME PLUGIN: found nameserver %s, cost %s", olsr_ip_to_string(&strbuf, &name->ip),
-                    get_linkcost_text(route->rt_best->rtp_metric.cost, true, &lqbuffer));
+          /* enqueue it on the head of list */
+          *nameserver_routes = route;
+          OLSR_PRINTF(6, "NAME PLUGIN: found nameserver %s, cost %s", olsr_ip_to_string(&strbuf, &name->ip),
+                      get_linkcost_text(route->rt_best->rtp_metric.cost, true, &lqbuffer));
 
-        /* find the closet one */
-        select_best_nameserver(nameserver_routes);
+          /* find the closet one */
+          select_best_nameserver(nameserver_routes);
+        }
       }
     }
   }
@@ -1643,10 +1646,11 @@ lookup_name_latlon(union olsr_ip_addr *ip)
     for (list_node = list_head->next; list_node != list_head; list_node = list_node->next) {
 
       entry = list2db(list_node);
-
-      for (name = entry->names; name != NULL; name = name->next) {
-        if (ipequal(&name->ip, ip))
-          return name->name;
+      if (entry) {
+        for (name = entry->names; name != NULL; name = name->next) {
+          if (ipequal(&name->ip, ip))
+            return name->name;
+        }
       }
     }
   }