Skip to content

Commit

Permalink
Bugfix: fetch_overlapping_subnets().
Browse files Browse the repository at this point in the history
A number of subnet columns have NULL'able types in the SCHEMA. e.g. vrfId can be NULL
or (integer) 0 for the default vrf depending on how it was created, edited or updated.

Update the SQL query to coalesce NULL values to 0 to include all records in the search.

Closes #3529
Closes #2793
  • Loading branch information
GaryAllan committed Mar 24, 2022
1 parent f55e4fb commit 272cbea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
23 changes: 14 additions & 9 deletions functions/classes/class.Subnets.php
Expand Up @@ -616,7 +616,7 @@ public function fetch_all_subnets() {
* @return array|false
*/
public function fetch_overlapping_subnets ($cidr, $method=null, $value=null, $result_fields = "*") {
if ($this->verify_cidr_address($cidr)!==true) return false;
if ($this->verify_cidr_address($cidr) !== true) return false;

$result_fields = $this->Database->escape_result_fields($result_fields);

Expand All @@ -626,22 +626,27 @@ public function fetch_overlapping_subnets ($cidr, $method=null, $value=null, $re
$cidr_broadcast = $this->decimal_broadcast_address($cidr_decimal, $cidr_mask);

$possible_parents = array();
for ($mask=0; $mask<=$cidr_mask; $mask++) {
for ($mask = 0; $mask <= $cidr_mask; $mask++) {
$parent = $this->decimal_network_address($cidr_decimal, $mask);
$possible_parents[] = "('$parent','$mask')";
}
$possible_parents = implode(',', $possible_parents);

$query = "SELECT $result_fields FROM `subnets` WHERE COALESCE(`isFolder`,0) = 0 AND ";
if (!is_null($method)) $query .= " `$method` = '".$this->Database->escape($value)."' AND ";
$query .= " ( ( LPAD(`subnet`,39,0) >= LPAD('$cidr_network',39,0) AND LPAD(`subnet`,39,0) <= LPAD('$cidr_broadcast',39,0) )";
$query .= " OR (`subnet`,`mask`) IN ($possible_parents) ) ";
$query .= "ORDER BY CAST(`mask` AS UNSIGNED) DESC, LPAD(`subnet`,39,0);";
$query = [];
$query[] = "SELECT $result_fields FROM `subnets` WHERE COALESCE(`isFolder`,0) = 0 AND ";
if (!is_null($method)) {
$query[] = " COALESCE(`$method`,0) = '" . $this->Database->escape($value) . "' AND ";
}
$query[] = " ( ";
$query[] = " ( LPAD(`subnet`,39,0) >= LPAD('$cidr_network',39,0) AND LPAD(`subnet`,39,0) <= LPAD('$cidr_broadcast',39,0) )";
$query[] = " OR (`subnet`,`mask`) IN ($possible_parents)";
$query[] = " ) ";
$query[] = "ORDER BY CAST(`mask` AS UNSIGNED) DESC, LPAD(`subnet`,39,0);";

try {
$overlaping_subnets = $this->Database->getObjectsQuery($query);
$overlaping_subnets = $this->Database->getObjectsQuery(implode("\n", $query));
} catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage());
$this->Result->show("danger", _("Error: ") . $e->getMessage());
return false;
}

Expand Down
6 changes: 6 additions & 0 deletions misc/CHANGELOG
@@ -1,3 +1,9 @@
== 1.4.6

Bugfixes:
----------------------------
+ Require unique subnets not working as intended (#3529);

== 1.4.5

Bugfixes:
Expand Down

0 comments on commit 272cbea

Please sign in to comment.