Fix supernet-calculations.ts

Invert the loop to correctly find the smallest CIDR.

Before this change, the loop in findSupernet starts at cidr = 1 and stops at the first CIDR that encompasses all addresses. Since /1 is large enough to cover any IPv4 range, it satisfies the condition (supernetStart ≤ minAddress and supernetEnd ≥ maxAddress) and is incorrectly selected.
This commit is contained in:
NoOne-dev committed 2025-10-20 11:02:09 -04:00
1 parent 380ee214db
commit 83770cfcdb
1 file changed
+1 -1
+1 -1
View File
@@ -152,7 +152,7 @@ function findSupernet(networks: NetworkInput[]): { network: string; cidr: number
// Find the smallest CIDR that can contain all networks
let supernetCidr = 32;
for (let cidr = 1; cidr <= 30; cidr++) {
for (let cidr = 30; cidr >= 1; cidr--) {
const mask = 0xffffffff << (32 - cidr);
const supernetStart = minAddress & (mask >>> 0);
const supernetEnd = supernetStart + Math.pow(2, 32 - cidr) - 1;