Fix insertion of subnets read from the subnets file: append to the *end* of
authorChristopher Maynard <Christopher.Maynard@igt.com>
Sun, 7 Jun 2015 00:04:42 +0000 (20:04 -0400)
committerAnders Broman <a.broman58@gmail.com>
Tue, 9 Jun 2015 04:09:52 +0000 (04:09 +0000)
the list.

The patch ensures that non-duplicate subnets are appended to the end of the
list rather than as the second element, which if there had been a second
element previously, the memory for it was effectively leaked.

It also allows /32 "subnets", even though arguably the hosts file should be
used instead, but now the test in read_subnets_file() matches the assert in
subnet_entry_set().

Bug: 11247
Change-Id: I54bf1cbb34edfcf410aa634043a377c27091df51
Reviewed-on: https://code.wireshark.org/review/8802
Petri-Dish: Jeff Morriss <jeff.morriss.ws@gmail.com>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Jeff Morriss <jeff.morriss.ws@gmail.com>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
epan/addr_resolv.c [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 37dd4b1..c580311
@@ -2087,8 +2087,8 @@ read_hosts_file (const char *hostspath, gboolean store_entries)
             /*
              * Add the aliases, too, if there are any.
              * XXX - except we only store the last one added.  The name
-            * resolver returns the first name in the hosts file, we should
-            * too.
+             * resolver returns the first name in the hosts file, we should
+             * too.
              */
             while ((cp = strtok(NULL, " \t")) != NULL) {
                 if (is_ipv6) {
@@ -2265,7 +2265,7 @@ read_subnets_file (const char *subnetspath)
         }
 
         mask_length = atoi(cp2);
-        if (0 >= mask_length || mask_length > 31) {
+        if (0 >= mask_length || mask_length > 32) {
             continue; /* invalid mask length */
         }
 
@@ -2352,13 +2352,19 @@ subnet_entry_set(guint32 subnet_addr, const guint32 mask_length, const gchar* na
     }
 
     if (NULL != (tp = entry->subnet_addresses[hash_idx])) {
-        if (tp->addr == subnet_addr) {
-            return;    /* XXX provide warning that an address was repeated? */
-        } else {
-            sub_net_hashipv4_t * new_tp = g_new(sub_net_hashipv4_t, 1);
-            tp->next = new_tp;
-            tp = new_tp;
+        sub_net_hashipv4_t * new_tp;
+
+        while (tp->next) {
+            if (tp->addr == subnet_addr) {
+                return; /* XXX provide warning that an address was repeated? */
+            } else {
+                tp = tp->next;
+            }
         }
+
+        new_tp = g_new(sub_net_hashipv4_t, 1);
+        tp->next = new_tp;
+        tp = new_tp;
     } else {
         tp = entry->subnet_addresses[hash_idx] = g_new(sub_net_hashipv4_t, 1);
     }