Index: trunk/cgi-bin/IPDB.pm
===================================================================
--- trunk/cgi-bin/IPDB.pm	(revision 654)
+++ trunk/cgi-bin/IPDB.pm	(revision 655)
@@ -1443,13 +1443,23 @@
         my $sth2 = $dbh->prepare("INSERT INTO poolips (ip,city,type,custid,parent_id) VALUES ".
           "(?,'$poolcity','$pooltype','$poolcustid',$poolid)");
+
 ##fixme:  need to not insert net, gateway, and bcast on "real netblock" pools (DHCPish)
         # don't insert .0
         $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.0$|;
-        foreach my $ip ($cidr->hostenum) {
-          $sth2->execute($ip);
+        $cidr++;
+        my $bcast = $cidr->broadcast;
+        while ($cidr != $bcast) {
+          $sth2->execute($cidr->addr);
+          $cidr++;
         }
-        $cidr--;
         # don't insert .255
         $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.255$|;
+
+# Weirdness Happens.  $cidr goes read-only somewhere (this is a thing?!?),
+# causing ->split, ->hostenum, and related methods to explode.  O_o
+#        foreach my $ip ($cidr->hostenum) {
+#          $sth2->execute($ip);
+#        }
+
       }
 
@@ -1461,8 +1471,12 @@
       else {
 
+# Edge case:  Block is the same size as more than one parent level.  Should be rare.
+# - mainly master + first routing.  Sorting on parent_id hides the problem pretty well,
+# but it's likely still possible to fail in particularly well-mangled databases.
+# The ultimate fix for this may be to resurrect the "routing depth" atrocity.  :/
         # Get all possible (and probably a number of impossible) containers for $cidr
         $sth = $dbh->prepare("SELECT cidr,parent_id,type,city,id FROM allocations ".
           "WHERE (type LIKE '_m' OR type LIKE '_c') AND cidr >>= ? AND master_id = ? ".
-          "ORDER BY masklen(cidr) DESC");
+          "ORDER BY masklen(cidr) DESC,parent_id DESC");
         $sth->execute($cidr, $binfo->{master_id});
 
@@ -1505,5 +1519,6 @@
         # Special case - delete pool IPs
         if ($binfo->{type} =~ /^.[pd]$/) {
-	  # We have to delete the IPs from the pool listing.
+          # We have to delete the IPs from the pool listing.
+##fixme:  rdepth?  vrf?
           $dbh->do("DELETE FROM poolips WHERE parent_id = ?", undef, ($id) );
         }
